使用场景

最近在做资源管理模块时,关于资源引用这方面的问题,并不想直接引用某一个asset
,比如Prefab、或者sprite、material等资源,当然可以由资源管理模块统一管理,然后对业务层提供获取的接口。由业务层传入文件名、或者AssetBundle
Name、path之类的数据,资源管理模块内加载出来然后返回。结合ObjectFiled这个编辑器类来介绍,我们拖入某一个asset,然后序列化存储其guid和asset
path,表面上看起来是引用了该asset,实际上只是存住了guid和path(用于load,结合资源管理模块而定),guid用于绝对指向asset资源,就算路径更改,也不怕引用丢失。

![在这里插入图片描述](https://img-blog.csdnimg.cn/20181222133749902.png?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI2OTk5NTA5,size_16,color_FFFFFF,t_70)

![在这里插入图片描述](https://img-blog.csdnimg.cn/20181222133618421.png?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI2OTk5NTA5,size_16,color_FFFFFF,t_70)

ObjectField介绍


https://docs.unity3d.com/2017.2/Documentation/ScriptReference/EditorGUI.ObjectField.html

FileField实现

private string[] FileField(Rect position, string label, string guid,Type type)
    {
        string[] guidPath = new string[2];//存储guid和path,这里的path是assetPath,实际存储起来需要做一步处理,处理成资源管理模块能load的path或者别的数据
        string path = AssetDatabase.GUIDToAssetPath(guid);
        UnityEngine.Object file = AssetDatabase.LoadAssetAtPath(path,type);
        EditorGUI.BeginChangeCheck();
        {
            //编辑器上显示的是ObjectField绘制出来的                                                                                  
            file = EditorGUI.ObjectField(position, label, file, type, false);
        }

        if (EditorGUI.EndChangeCheck())
        {
            path = AssetDatabase.GetAssetPath(file);
            guid = AssetDatabase.AssetPathToGUID(path);
        }

        guidPath[0] = guid;
        guidPath[1] = path;
        return guidPath;
    }

不算完整的代码:

using UnityEditor;
using UnityEngine;
using System;
using System.IO;
using IdleGames.Core.AssetManagement;

[CustomPropertyDrawer(typeof(WeakReferenceAttribute), true)]
public class WeakReferenceBaseDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        WeakReferenceAttribute attribute = (WeakReferenceAttribute)base.attribute;
        Type referenceType = typeof(GameObject);
        if (attribute != null)
        {
            referenceType = attribute.AssetType;
        }

        SerializedProperty guidProperty = property.FindPropertyRelative("_guid");
        SerializedProperty pathProperty = property.FindPropertyRelative("_path");
        Rect guidRect = position;

        string[] guidPath = FileField(guidRect, property.displayName, guidProperty.stringValue,referenceType);
        guidProperty.stringValue = guidPath[0];
        //这里演示的是把path存成了Resources文件夹下的path
        if (string.IsNullOrEmpty(guidPath[1]) == false && guidPath[1].Contains("Resources/"))
        {
            int index = guidPath[1].LastIndexOf("Resources/") + "Resources/".Length;
            string path = guidPath[1].Substring(index);
            path = path.Remove(path.LastIndexOf("."));
            pathProperty.stringValue = path;
        }
        property.serializedObject.ApplyModifiedProperties();
    }

    private string[] FileField(Rect position, string label, string guid,Type type)
    {
        string[] guidPath = new string[2];
        string path = AssetDatabase.GUIDToAssetPath(guid);
        UnityEngine.Object file = AssetDatabase.LoadAssetAtPath(path,type);
        EditorGUI.BeginChangeCheck();
        {                                                                                  
            file = EditorGUI.ObjectField(position, label, file, type, false);
        }

        if (EditorGUI.EndChangeCheck())
        {
            path = AssetDatabase.GetAssetPath(file);
            guid = AssetDatabase.AssetPathToGUID(path);
        }

        guidPath[0] = guid;
        guidPath[1] = path;
        return guidPath;
    }
}

上述代码仅供参考,拖入的object与实际序列化存储的不是同一个东西时,可以用上述类似的思路

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。