Coder Social home page Coder Social logo

unityserializableinterface's Introduction

Serializable Interface

GitHub package.json version GitHub issues GitHub pull requests GitHub license GitHub last commit

A wrapper that allows you to serialize interfaces. Both UnityEngine.Object and regular object implementers work!

Installation

  1. The package is available on the openupm registry. You can install it via openupm-cli.
openupm add net.tnrd.serializableinterface
  1. Installing through a Unity Package created by the Package Installer Creator from Needle

Usage

Usage is pretty easy and straightforward. Assuming you have the following interface

public interface IMyInterface
{
    void Greet();
}

You can add it to a behaviour like so

using TNRD;
using UnityEngine;

public class MyBehaviour : MonoBehaviour
{
    [SerializeField] private SerializableInterface<IMyInterface> mySerializableInterface;

    private void Awake()
    {
        mySerializableInterface.Value.Greet();
    }
}

Back in the Unity inspector it will look like this

image

And when you click on the object selector button you will be shown a dropdown window with all possible options like this

image

As you can see you can select items from multiple locations:

  • Assets (Scriptable Objects and Prefabs that implement said interface)
  • Classes (custom classes that implement said interface)
  • Scene (objects in the scene that implement said interface)

For the sake of example for the image above I have created some simple implementations

using UnityEngine;

public class MyComponent : MonoBehaviour, IMyInterface
{
    /// <inheritdoc />
    public void Greet()
    {
        Debug.Log("Hello, World! I'm MyComponent");
    }
}
using UnityEngine;

public class MyPoco : IMyInterface
{
    /// <inheritdoc />
    public void Greet()
    {
        Debug.Log("Hello, World! I'm MyPoco");
    }
}
using UnityEngine;

[CreateAssetMenu(menuName = "MyScriptable")]
public class MyScriptable : ScriptableObject, IMyInterface
{
    /// <inheritdoc />
    public void Greet()
    {
        Debug.Log("Hello, World! I'm MyScriptable");
    }
}

Support

Serializable Interface is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.

ko-fi

Contributions

Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.

unityserializableinterface's People

Contributors

dependabot[bot] avatar marc-antoine-girard avatar mnicolas94 avatar semantic-release-bot avatar simonkellly avatar thundernerd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.