|
1 | | -# Unity3D-SerializableInterface |
| 1 | +# Serializable Interface |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <img alt="GitHub package.json version" src ="https://img.shields.io/github/package-json/v/Thundernerd/Unity3D-SeriAlizableInterface" /> |
| 5 | + <a href="https://github.com/Thundernerd/Unity3D-SeriAlizableInterface/issues"> |
| 6 | + <img alt="GitHub issues" src ="https://img.shields.io/github/issues/Thundernerd/Unity3D-SeriAlizableInterface" /> |
| 7 | + </a> |
| 8 | + <a href="https://github.com/Thundernerd/Unity3D-SeriAlizableInterface/pulls"> |
| 9 | + <img alt="GitHub pull requests" src ="https://img.shields.io/github/issues-pr/Thundernerd/Unity3D-SeriAlizableInterface" /> |
| 10 | + </a> |
| 11 | + <a href="https://github.com/Thundernerd/Unity3D-SeriAlizableInterface/blob/master/LICENSE.md"> |
| 12 | + <img alt="GitHub license" src ="https://img.shields.io/github/license/Thundernerd/Unity3D-SeriAlizableInterface" /> |
| 13 | + </a> |
| 14 | + <img alt="GitHub last commit" src ="https://img.shields.io/github/last-commit/Thundernerd/Unity3D-SeriAlizableInterface" /> |
| 15 | +</p> |
| 16 | + |
| 17 | +A wrapper that allows you to serialize interfaces. Both UnityEngine.Object and regular object implementers work! |
| 18 | + |
| 19 | +## Installation |
| 20 | +1. The package is available on the [openupm registry](https://openupm.com). You can install it via [openupm-cli](https://github.com/openupm/openupm-cli). |
| 21 | +``` |
| 22 | +openupm add net.tnrd.serializableinterface |
| 23 | +``` |
| 24 | + |
| 25 | +2. Installing through a [Unity Package](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.serializableinterface?registry=https://package.openupm.com) created by the [Package Installer Creator](https://package-installer.glitch.me) from [Needle](https://needle.tools) |
| 26 | + |
| 27 | +[<img src="https://img.shields.io/badge/-Download-success?style=for-the-badge"/>](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.serializableinterface?registry=https://package.openupm.com) |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +Usage is pretty easy and straightforward. Assuming you have the following interface |
| 32 | +```c# |
| 33 | +public interface IMyInterface |
| 34 | +{ |
| 35 | + void Greet(); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +You can add it to a behaviour like so |
| 40 | +```c# |
| 41 | +using TNRD; |
| 42 | +using UnityEngine; |
| 43 | + |
| 44 | +public class MyBehaviour : MonoBehaviour |
| 45 | +{ |
| 46 | + [SerializeField] private SerializableInterface<IMyInterface> mySerializableInterface; |
| 47 | + |
| 48 | + private void Awake() |
| 49 | + { |
| 50 | + mySerializableInterface.Value.Greet(); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Back in the Unity inspector it will look like this |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +And when you click on the object selector button you will be shown a dropdown window with all possible options like this |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +As you can see you can select items from multiple locations: |
| 64 | +- Assets (Scriptable Objects that implement said interface) |
| 65 | +- Classes (custom classes that implement said interface) |
| 66 | +- Scene (objects in the scene that implement said interface) |
| 67 | + |
| 68 | +For the sake of example for the image above I have created some simple implementations |
| 69 | + |
| 70 | +```c# |
| 71 | +using UnityEngine; |
| 72 | + |
| 73 | +public class MyComponent : MonoBehaviour, IMyInterface |
| 74 | +{ |
| 75 | + /// <inheritdoc /> |
| 76 | + public void Greet() |
| 77 | + { |
| 78 | + Debug.Log("Hello, World! I'm MyComponent"); |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +```c# |
| 84 | +using UnityEngine; |
| 85 | + |
| 86 | +public class MyPoco : IMyInterface |
| 87 | +{ |
| 88 | + /// <inheritdoc /> |
| 89 | + public void Greet() |
| 90 | + { |
| 91 | + Debug.Log("Hello, World! I'm MyPoco"); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```c# |
| 97 | +using UnityEngine; |
| 98 | + |
| 99 | +[CreateAssetMenu(menuName = "MyScriptable")] |
| 100 | +public class MyScriptable : ScriptableObject, IMyInterface |
| 101 | +{ |
| 102 | + /// <inheritdoc /> |
| 103 | + public void Greet() |
| 104 | + { |
| 105 | + Debug.Log("Hello, World! I'm MyScriptable"); |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | + |
| 111 | +## Support |
| 112 | +**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. |
| 113 | + |
| 114 | +[](https://ko-fi.com/J3J11GEYY) |
| 115 | + |
| 116 | +## Contributions |
| 117 | +Pull requests are welcomed. Please feel free to fix any issues you find, or add new features. |
| 118 | + |
0 commit comments