Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using MLAPI;
  5. using MLAPI.NetworkedVar;
  6. using UnityEngine;
  7.  
  8. namespace UnityEditor
  9. {
  10. [CustomEditor(typeof(NetworkedBehaviour), true)]
  11. [CanEditMultipleObjects]
  12. public class NetworkedBehaviourEditor : Editor
  13. {
  14. private bool initialized;
  15. private List<string> networkedVarNames = new List<string>();
  16. private Dictionary<string, FieldInfo> networkedVarFields = new Dictionary<string, FieldInfo>();
  17. private Dictionary<string, object> networkedVarObjects = new Dictionary<string, object>();
  18.  
  19. private GUIContent networkedVarLabelGuiContent;
  20.  
  21. private void Init(MonoScript script)
  22. {
  23. initialized = true;
  24.  
  25. networkedVarNames.Clear();
  26. networkedVarFields.Clear();
  27. networkedVarObjects.Clear();
  28.  
  29. networkedVarLabelGuiContent = new GUIContent("NetworkedVar", "This variable is a NetworkedVar. It can not be serialized and can only be changed during runtime.");
  30.  
  31. FieldInfo[] fields = script.GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
  32. for (int i = 0; i < fields.Length; i++)
  33. {
  34. Type ft = fields[i].FieldType;
  35. if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(NetworkedVar<>))
  36. {
  37. networkedVarNames.Add(fields[i].Name);
  38. networkedVarFields.Add(fields[i].Name, fields[i]);
  39. }
  40. }
  41. }
  42.  
  43. void RenderNetworkedVar(int index)
  44. {
  45. if (!networkedVarFields.ContainsKey(networkedVarNames[index]))
  46. {
  47. serializedObject.Update();
  48. SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
  49. if (scriptProperty == null)
  50. return;
  51.  
  52. MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
  53. Init(targetScript);
  54. }
  55.  
  56. object value = networkedVarFields[networkedVarNames[index]].GetValue(target);
  57. if (value == null)
  58. {
  59. Type fieldType = networkedVarFields[networkedVarNames[index]].FieldType;
  60. Type genType = fieldType.MakeGenericType(fieldType.GetGenericArguments());
  61. INetworkedVar var = (INetworkedVar) Activator.CreateInstance(genType, true);
  62. networkedVarFields[networkedVarNames[index]].SetValue(target, var);
  63. }
  64.  
  65. Type type = networkedVarFields[networkedVarNames[index]].GetValue(target).GetType();
  66. Type genericType = type.GetGenericArguments()[0];
  67.  
  68. EditorGUILayout.BeginHorizontal();
  69. if (genericType == typeof(string))
  70. {
  71. NetworkedVar<string> var = (NetworkedVar<string>)networkedVarFields[networkedVarNames[index]].GetValue(target);
  72. var.Value = EditorGUILayout.TextField(networkedVarNames[index], var.Value);
  73. }
  74. else if (genericType.IsValueType)
  75. {
  76. MethodInfo method = typeof(NetworkedBehaviourEditor).GetMethod("RenderNetworkedVarValueType", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
  77. MethodInfo genericMethod = method.MakeGenericMethod(genericType);
  78. genericMethod.Invoke(this, new object[] { (object)index });
  79. }
  80. else
  81. {
  82. EditorGUILayout.LabelField("Type not renderable");
  83. }
  84. GUILayout.Label(networkedVarLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(networkedVarLabelGuiContent).x));
  85. EditorGUILayout.EndHorizontal();
  86. }
  87.  
  88. void RenderNetworkedVarValueType<T>(int index) where T : struct
  89. {
  90. NetworkedVar<T> var = (NetworkedVar<T>)networkedVarFields[networkedVarNames[index]].GetValue(target);
  91. Type type = typeof(T);
  92. object val = var.Value;
  93. string name = networkedVarNames[index];
  94. if (type == typeof(int))
  95. val = EditorGUILayout.IntField(name, (int)val);
  96. else if (type == typeof(uint))
  97. val = (uint)EditorGUILayout.LongField(name, (long)((uint)val));
  98. else if (type == typeof(short))
  99. val = (short)EditorGUILayout.IntField(name, (int)((short)val));
  100. else if (type == typeof(ushort))
  101. val = (ushort)EditorGUILayout.IntField(name, (int)((ushort)val));
  102. else if (type == typeof(sbyte))
  103. val = (sbyte)EditorGUILayout.IntField(name, (int)((sbyte)val));
  104. else if (type == typeof(byte))
  105. val = (byte)EditorGUILayout.IntField(name, (int)((byte)val));
  106. else if (type == typeof(long))
  107. val = EditorGUILayout.LongField(name, (long)val);
  108. else if (type == typeof(ulong))
  109. val = (ulong)EditorGUILayout.LongField(name, (long)((ulong)val));
  110. else if (type == typeof(bool))
  111. val = EditorGUILayout.Toggle(name, (bool)val);
  112. else if (type == typeof(string))
  113. val = EditorGUILayout.TextField(name, (string)val);
  114. else
  115. EditorGUILayout.LabelField("Type not renderable");
  116.  
  117. var.Value = (T)val;
  118. }
  119.  
  120. public override void OnInspectorGUI()
  121. {
  122. if (!initialized)
  123. {
  124. serializedObject.Update();
  125. SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
  126. if (scriptProperty == null)
  127. return;
  128.  
  129. MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
  130. Init(targetScript);
  131. }
  132.  
  133. EditorGUI.BeginChangeCheck();
  134. serializedObject.Update();
  135.  
  136. for (int i = 0; i < networkedVarNames.Count; i++)
  137. RenderNetworkedVar(i);
  138.  
  139. SerializedProperty property = serializedObject.GetIterator();
  140. bool expanded = true;
  141. while (property.NextVisible(expanded))
  142. {
  143. if (property.propertyType == SerializedPropertyType.ObjectReference)
  144. {
  145. if (property.name == "m_Script")
  146. EditorGUI.BeginDisabledGroup(true);
  147.  
  148. EditorGUILayout.PropertyField(property, true);
  149.  
  150. if (property.name == "m_Script")
  151. EditorGUI.EndDisabledGroup();
  152. }
  153. else
  154. {
  155. EditorGUILayout.BeginHorizontal();
  156. EditorGUILayout.PropertyField(property, true);
  157. EditorGUILayout.EndHorizontal();
  158. }
  159. expanded = false;
  160. }
  161. serializedObject.ApplyModifiedProperties();
  162. EditorGUI.EndChangeCheck();
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement