wangguan
2020-12-07 c145f328f98f237ac3d52e229fdd1eb51218222d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
 
namespace Core.Utilities
{
    /// <summary>
    /// Abstract base for serializable interface wrapper objects
    /// </summary>
    public abstract class SerializableInterface
    {
        /// <summary>
        /// Unity component that gets serialized that is of our interface type
        /// </summary>
        public UnityEngine.Object unityObjectReference;
    }
 
    /// <summary>
    /// A generic solution to allow the serialization of interfaces in Unity game objects
    /// </summary>
    /// <typeparam name="T">Any interface implementing ISerializableInterface</typeparam>
    [Serializable]
    public class SerializableInterface<T> : SerializableInterface where T: ISerializableInterface
    {
        T m_InterfaceReference;
        
        /// <summary>
        /// Retrieves the interface from the unity component and caches it
        /// </summary>
        public T GetInterface()
        {
            if (m_InterfaceReference == null && unityObjectReference != null)
            {
                m_InterfaceReference = (T)(ISerializableInterface)unityObjectReference;
            }
 
            return m_InterfaceReference;
        }
    }
 
    /// <summary>
    /// Base interface from which all serializable interfaces must derive
    /// </summary>
    public interface ISerializableInterface
    {
    }
}