Esempio n. 1
0
    internal static BlobAssetReference <T> LoadAsset <T>(WeakAssetReference assetRef) where T : struct
    {
        var path = GetBlobAssetPath(assetRef.GetGuidStr());

        if (!File.Exists(path))
        {
            throw new FileNotFoundException("Cannot find file:" + path);
        }

        return(BlobAssetReference <T> .Create(File.ReadAllBytes(path)));
    }
Esempio n. 2
0
//    public Object LoadSingleAssetResource(string guid)
//    {
//        if (guid == null || guid == "")
//        {
//            GameDebug.LogError("Guid invalid");
//            return null;
//        }
//
//        var reference = new WeakAssetReference(guid);
//        return GetSingleAssetResource(reference);
//    }

    public Object GetSingleAssetResource(WeakAssetReference reference)
    {
        var def = new SingleResourceBundle();

        if (m_resources.TryGetValue(reference, out def))
        {
            return(def.asset);
        }

        def = new SingleResourceBundle();
        var useBundles = !Application.isEditor || forceBundles.IntValue > 0;

        var guidStr = reference.GetGuidStr();

#if UNITY_EDITOR
        if (!useBundles)
        {
            var path = AssetDatabase.GUIDToAssetPath(guidStr);

            def.asset = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object));

            if (def.asset == null)
            {
                GameDebug.LogWarning("Failed to load resource " + guidStr + " at " + path);
            }
            if (verbose.IntValue > 0)
            {
                GameDebug.Log("resource: loading non-bundled asset " + path + "(" + guidStr + ")");
            }
        }
#endif
        if (useBundles)
        {
            var bundlePath = GetBundlePath();
            def.bundle = AssetBundle.LoadFromFile(bundlePath + "/" + m_assetResourceFolder + "/" + guidStr);
            if (verbose.IntValue > 0)
            {
                GameDebug.Log("resource: loading bundled asset: " + m_assetResourceFolder + "/" + guidStr);
            }
            var handles = def.bundle.LoadAllAssets();
            if (handles.Length > 0)
            {
                def.asset = handles[0];
            }
            else
            {
                GameDebug.LogWarning("Failed to load resource " + guidStr);
            }
        }

        m_resources.Add(reference, def);
        return(def.asset);
    }
    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {
        // Figure out what asset types we allow. Default to all.
        AssetTypeAttribute assetTypeProperty = System.Attribute.GetCustomAttribute(fieldInfo, typeof(AssetTypeAttribute)) as AssetTypeAttribute;
        var assetType = assetTypeProperty != null ? assetTypeProperty.assetType : typeof(Object);

        var val0 = prop.FindPropertyRelative("val0");
        var val1 = prop.FindPropertyRelative("val1");
        var val2 = prop.FindPropertyRelative("val2");
        var val3 = prop.FindPropertyRelative("val3");

        var reference = new WeakAssetReference(val0.intValue, val1.intValue, val2.intValue, val3.intValue);

        var    guidStr = "";
        Object obj     = null;

        if (reference.IsSet())
        {
            guidStr = reference.GetGuidStr();
            var path = AssetDatabase.GUIDToAssetPath(guidStr);

            if (assetType == null)
            {
                assetType = AssetDatabase.GetMainAssetTypeAtPath(path);
            }

            obj = AssetDatabase.LoadAssetAtPath(path, assetType);
        }

        pos = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(label.text + "(" + guidStr + ")"));
        Object newObj = EditorGUI.ObjectField(pos, obj, assetType, false);

        if (newObj != obj)
        {
            var newRef = new WeakAssetReference();
            if (newObj != null)
            {
                var path = AssetDatabase.GetAssetPath(newObj);
                newRef = new WeakAssetReference(AssetDatabase.AssetPathToGUID(path));
            }

            val0.intValue = newRef.val0;
            val1.intValue = newRef.val1;
            val2.intValue = newRef.val2;
            val3.intValue = newRef.val3;
        }
    }