Пример #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var serializedProperty = property.FindPropertyRelative("_path");
            var path           = serializedProperty.stringValue;
            var serializedType = property.FindPropertyRelative("_serializedType");
            var type           = GetResourceType(serializedType);

            if (type == null)
            {
                type = Type.GetType(serializedType.stringValue);
            }
            if (type != null)
            {
                serializedType.stringValue = ResourceReference.SerializeType(type);
            }
            var lastValue = Resources.Load(path);

            if (lastValue == null)
            {
                lastValue = Recovery(property);
            }

            var guidProperty = property.FindPropertyRelative("_guid");

            label.text    = label.text + ": " + (type != null ? type.Name : string.Empty) + string.Format(" [{0}]", string.IsNullOrEmpty(path) ? "null" : path);
            label.tooltip = label.text;
            var newValue = EditorGUI.ObjectField(position, label, lastValue, type, false);

            guidProperty.stringValue       = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(newValue));
            serializedProperty.stringValue = ResourceReferenceEditorUtils.GetResourcePath(newValue);
        }
Пример #2
0
        private static UnityEngine.Object Recovery(SerializedProperty property)
        {
            if (property != null)
            {
                var guidProperty = property.FindPropertyRelative("_guid");

                var assetPath = AssetDatabase.GUIDToAssetPath(guidProperty.stringValue);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    int rIndex = assetPath.LastIndexOf("Resources");
                    if (rIndex != -1)
                    {
                        assetPath = assetPath.Remove(0, rIndex + "Resources".Length).Trim('/', '\\', ' ', '\t', '\r', '\n');
                        assetPath = Path.ChangeExtension(assetPath, "").Trim('.', ',');
                        var newValue = Resources.Load(assetPath);
                        guidProperty.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(newValue));
                        property.FindPropertyRelative("_path").stringValue = ResourceReferenceEditorUtils.GetResourcePath(newValue);
                        return(newValue);
                    }
                }
            }

            return(null);
        }