Example #1
0
 public string GetText()
 {
     System.Object obj = null;
     if (!CacheTable.TryGetValue(ASSETTEXT, out obj))
     {
         obj = www.downloadHandler.text;
         CacheTable.Add(ASSETTEXT, obj);
     }
     if (obj != null)
     {
         return(obj.ToString());
     }
     else
     {
         return(string.Empty);
     }
 }
 // draw the default element
 public void DrawElement(Rect rect, SerializedProperty element, System.Object listItem, bool selected, bool focused, bool draggable)
 {
     EditorGUI.LabelField(rect, EditorGUIUtility.TempContent((element != null) ? element.displayName : listItem.ToString()));
 }
        protected void OnComponentGUI(Rect position, SerializedProperty prop, GUIContent label, string fieldName, string[] requiredValues, string defaultObject, bool isPrefab, int rightOffset, System.Type overrideComponentType = null)
        {
            if (prop.propertyType != SerializedPropertyType.ObjectReference)
            {
                WrongVariableTypeWarning("ComponentSelection", "object references");
                return;
            }

            int originalIndentLevel = EditorGUI.indentLevel;

            GUIContent tempLabel = new GUIContent(label);

            tempLabel.tooltip     = "1st Line : GameObject Reference. The GameObject on which to select a Component. This is not Serialized.\n\n2nd Line : The Selected Component. This is your actual Serialized variable.\n\n(GameObject reference is displayed in YELLOW color when the selected component is null, WHITE whe the selected component is on the same GameObject as this script and CYAN when the selected component is on another GameObject.)";
            position              = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), tempLabel);
            EditorGUI.indentLevel = 0;

            if (prop.hasMultipleDifferentValues)
            {
                position.height = GetPropertyHeight(prop, label);

                                #if !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
                if (fieldInfo.FieldType.IsArray || fieldInfo.FieldType.IsGenericType)
                {
                    position.x += 15;
                }
                                #endif
                EditorGUI.HelpBox(position, "Multi object editing is not supported for references with different values.", MessageType.Warning);
                return;
            }

            bool firstInit = false;
            if (!targetObjects.ContainsKey(prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath))
            {
                firstInit = true;
                targetObjects.Add(prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath, null);
            }

            // Set back the target game object if the drawed object has been deselected.
            if (prop.objectReferenceValue != null && (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null || targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] != (prop.objectReferenceValue as Component).gameObject))
            {
                targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = (prop.objectReferenceValue as Component).gameObject;
            }

            if (firstInit && targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null)
            {
                firstInit = false;
                try
                {
                    targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = string.IsNullOrEmpty(defaultObject) ? (prop.serializedObject.targetObject as MonoBehaviour).gameObject : isPrefab?Resources.Load(defaultObject) as GameObject : GameObject.Find(defaultObject);
                }
                catch
                {
                    targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = string.IsNullOrEmpty(defaultObject) ? null : isPrefab?Resources.Load(defaultObject) as GameObject : null;
                }
            }

            if ((prop.serializedObject.targetObject as ScriptableObject) != null)
            {
                if (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] != null && !AssetDatabase.Contains(targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath]))
                {
                    targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = null;
                }
            }

            position.height /= 2;
            position.width  -= rightOffset;

            EditorGUI.BeginChangeCheck();

            Color tempColor = GUI.color;
            try{
                GUI.color = targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == (prop.serializedObject.targetObject as MonoBehaviour).gameObject ? objectSelfColor : objectOtherColor;
            }
            catch {
                GUI.color = objectOtherColor;
            }

            if (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null)
            {
                GUI.color = objectNullColor;
            }

            targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = EditorGUI.ObjectField(position, targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath], typeof(GameObject), true) as GameObject;

            GUI.color = tempColor;

            if (targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] == null)
            {
                prop.objectReferenceValue = null;
                return;
            }

            position.width += rightOffset;

            System.Type      componentType;
            List <Component> components;
            if (fieldInfo.FieldType.IsArray)
            {
                componentType = fieldInfo.FieldType.GetElementType();
            }
            else if (fieldInfo.FieldType.IsGenericType)
            {
                componentType = fieldInfo.FieldType.GetGenericArguments()[0];
            }
            else
            {
                componentType = fieldInfo.FieldType;
            }

            if (overrideComponentType != null)
            {
                componentType = overrideComponentType;
            }

            try
            {
                components = targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath].GetComponents(componentType).ToList();
            }
            catch
            {
                components = targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath].gameObject.GetComponents(componentType).ToList();
            }

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObjects(prop.serializedObject.targetObjects, "Change Target Object");

                prop.objectReferenceValue = null;

                foreach (Object obj in prop.serializedObject.targetObjects)
                {
                    EditorUtility.SetDirty(obj);
                }
            }

            if (components.Contains(prop.serializedObject.targetObject as Component))
            {
                components.Remove(prop.serializedObject.targetObject as Component);
            }

            List <string> componentsNames = new List <string>();
            Dictionary <System.Type, int> componentsNumbers          = new Dictionary <System.Type, int>();
            Dictionary <string, int>      namedMonoBehavioursNumbers = new Dictionary <string, int>();
            List <Component> markedForDeletion = new List <Component>();

            foreach (Component component in components)
            {
                if (component == null)
                {
                    continue;
                }

                if (!componentsNumbers.ContainsKey(component.GetType()))
                {
                    componentsNumbers.Add(component.GetType(), 1);
                }

                if (component is NamedMonoBehaviour)
                {
                    if (string.IsNullOrEmpty((component as NamedMonoBehaviour).scriptName))
                    {
                        if (string.IsNullOrEmpty(fieldName))
                        {
                            componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString());
                        }
                        else
                        {
                            System.Object val = GetFieldOrPropertyValue(component, fieldName);

                            if (requiredValues != null && requiredValues.Length > 0)
                            {
                                if (requiredValues.Contains(val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")))
                                {
                                    componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")");
                                }
                                else
                                {
                                    markedForDeletion.Add(component);
                                }
                            }
                            else
                            {
                                componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")");
                            }
                        }
                    }
                    else
                    {
                        if (namedMonoBehavioursNumbers.ContainsKey((component as NamedMonoBehaviour).scriptName))
                        {
                            (component as NamedMonoBehaviour).scriptName += (" " + namedMonoBehavioursNumbers[(component as NamedMonoBehaviour).scriptName]++);
                        }
                        else
                        {
                            namedMonoBehavioursNumbers.Add((component as NamedMonoBehaviour).scriptName, 2);
                        }

                        if (string.IsNullOrEmpty(fieldName))
                        {
                            componentsNames.Add((component as NamedMonoBehaviour).scriptName);
                        }
                        else
                        {
                            System.Object val = GetFieldOrPropertyValue(component, fieldName);
                            if (requiredValues != null && requiredValues.Length > 0)
                            {
                                if (requiredValues.Contains(val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")))
                                {
                                    componentsNames.Add((component as NamedMonoBehaviour).scriptName + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")");
                                }
                                else
                                {
                                    markedForDeletion.Add(component);
                                }
                            }
                            else
                            {
                                componentsNames.Add((component as NamedMonoBehaviour).scriptName + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")");
                            }
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(fieldName))
                    {
                        componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString());
                    }
                    else
                    {
                        System.Object val = GetFieldOrPropertyValue(component, fieldName);
                        if (requiredValues != null && requiredValues.Length > 0)
                        {
                            if (requiredValues.Contains(val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")))
                            {
                                componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")");
                            }
                            else
                            {
                                markedForDeletion.Add(component);
                            }
                        }
                        else
                        {
                            componentsNames.Add(component.GetType().ToString().Replace("UnityEngine.", "") + " " + componentsNumbers[component.GetType()]++.ToString() + " (" + (val == null ? "null" : val.ToString().Replace(" (" + val.GetType().ToString() + ")", "")) + ")");
                        }
                    }
                }
            }

            foreach (Component component in markedForDeletion)
            {
                components.Remove(component);
            }

            if (components.Count == 0)
            {
                targetObjects[prop.serializedObject.targetObject.GetHashCode().ToString() + prop.propertyPath] = null;
                prop.objectReferenceValue = null;
                return;
            }

            components.Insert(0, null);
            componentsNames.Insert(0, "None (" + componentType.ToString().Replace("UnityEngine.", "") + ")");

            position.y += position.height;

            int index = 0;

            try
            {
                index = components.IndexOf(prop.objectReferenceValue as Component);
            }
            catch
            {
                prop.objectReferenceValue = null;
            }

            try
            {
                if (index != 0 && typeof(NamedMonoBehaviour).IsAssignableFrom(components[index].GetType()))
                {
                    GUI.backgroundColor = (components[index] as NamedMonoBehaviour).scriptNameColor;
                }

                prop.objectReferenceValue = components[EditorGUI.Popup(position, index, componentsNames.ToArray())];

                GUI.backgroundColor = Color.white;
            }
            catch
            {
            }

            EditorGUI.indentLevel = originalIndentLevel;
        }