-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceReference.cs
More file actions
135 lines (112 loc) · 3.54 KB
/
InterfaceReference.cs
File metadata and controls
135 lines (112 loc) · 3.54 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Author: Alessandro Salani (Cippman)
*/
using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace CippSharp.Core.Interfaces
{
/// <summary>
/// Create a custom serializable class that inherit from this class to reference an interface of type I
/// I must be an interface.
/// Useful to hold reference to components/objects of type t;
/// </summary>
/// <typeparam name="I"></typeparam>
[Serializable]
public abstract class InterfaceReference<I> : ISerializationCallbackReceiver where I : class
{
private const string None = "None";
[SerializeField, NotEditable] private string name = null;
/// <summary>
/// Exposed field assignable from inspector
/// </summary>
[SerializeField] private Object target = null;
/// <summary>
/// The target.
/// </summary>
public Object Target
{
get { return target; }
set
{
SetTarget(value);
SetName(target);
}
}
private void SetTarget(Object value)
{
if (value == null)
{
target = null;
return;
}
if (value is I)
{
target = value;
}
else
{
target = null;
Debug.LogWarning(string.Format("You should set an Object that implements <i>{0}</i>.", typeof(I).FullName));
}
}
private void SetName(Object obj)
{
name = obj != null ? string.Format("{0} ({1})", obj.name, typeof(I).Name) : None;
}
/// <summary>
/// The interface.
/// </summary>
public I Interface
{
get
{
return Target != null ? (I)(object)Target : null;
}
}
#region ISerializationCallbackReceiver Implementation
public void OnBeforeSerialize()
{
CheckTarget();
SetName(Target);
}
private void CheckTarget()
{
if (Target == null)
{
return;
}
//For a comfy Drag n drop of a GameObject
if (Target is GameObject)
{
GameObject gameObject = (GameObject) target;
Target = gameObject.GetComponent(typeof(I));
if (Target == null)
{
Debug.LogWarning(string.Format("You must assign a gameObject that have at least one component of type <i>{0}</i>.", typeof(I).FullName));
return;
}
}
if (!(Target is I))
{
//skip assignment by property during edit mode.
target = null;
Debug.LogWarning(string.Format("You must assign an Object that implements <i>{0}</i>.", typeof(I).FullName));
}
}
public void OnAfterDeserialize()
{
}
#endregion
#region Operators
public static implicit operator I(InterfaceReference<I> interfaceReference)
{
return interfaceReference.Interface;
}
#endregion
public override string ToString()
{
return typeof(InterfaceReference<I>).Name + " " + name;
}
}
}