private void SetBundleName(int index)
    {
        AssetBundlePermanentAsset script = target as AssetBundlePermanentAsset;

        UnityEngine.Object folderObj     = script.BundleNameList[index].bundleFolder;
        UnityEngine.Object lastFolderObj = script.BundleNameList[index].lastBundleFolder;
        if (folderObj != null && folderObj != lastFolderObj)
        {
            script.BundleNameList[index].bundleName       = AssetBundlePermanentAsset.ERROR_TIPS;
            script.BundleNameList[index].lastBundleFolder = folderObj;
            string path = AssetDatabase.GetAssetPath(folderObj);                            // 获取资源路径
            // 判断是不是文件夹
            if (Directory.Exists(path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                // 遍历子物体,直到找到一个有ab名的资源
                foreach (var childSysInfo in directoryInfo.GetFileSystemInfos())
                {
                    if (childSysInfo != null && childSysInfo.Extension != ".meta" && childSysInfo is FileInfo)
                    {
                        string        unityPath     = childSysInfo.FullName.Replace('\\', '/'); //反斜杠替换成斜杠
                        AssetImporter assetImporter = AssetImporter.GetAtPath(unityPath.Substring(unityPath.IndexOf("Assets")));
                        if (assetImporter != null && !string.IsNullOrEmpty(assetImporter.assetBundleName))
                        {
                            script.BundleNameList[index].bundleName = assetImporter.assetBundleName + "." + assetImporter.assetBundleVariant;
                            break;
                        }
                    }
                }//foreach_end
            }
        }
    }// fucntion_end
    }// fucntion_end

    // OnEnable的时候清空上一次obj,以便与刷新bundleName
    private void ClearLastFolderObj()
    {
        AssetBundlePermanentAsset script = target as AssetBundlePermanentAsset;

        for (int i = 0; i < script.BundleNameList.Count; i++)
        {
            script.BundleNameList[i].lastBundleFolder = null;
        }
    }
Exemplo n.º 3
0
        /// <summary>
        /// 常驻内存AssetConfig加载(AssetBundleMgr初始化之后调用)
        /// </summary>
        public void LoadAssetBundlePermanentAsset()
        {
            string abName    = "assetconfigs.u3dassetbundle";
            string assetName = "AssetBundlePermanentAsset";

            AssetBundleMgr.Instance.LoadBundleAsset(abName, assetName, (obj) =>
            {
                if (obj != null)
                {
                    abPermanentAsset = obj as AssetBundlePermanentAsset;
                }
                else
                {
                    Debug.LogError("常驻内存ab包配置加载失败");
                }
            }, false);
        }
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        AssetBundlePermanentAsset script = target as AssetBundlePermanentAsset;

        // GameObject列表
        SerializedProperty prop = serializedObject.FindProperty("BundleNameList");

        // 绘制子物体回调
        reorderableList.drawElementCallback = (rect, index, isActive, isFocused) =>
        {
            SetBundleName(index);
            EditorGUI.PropertyField(rect, prop.GetArrayElementAtIndex(index));
        };

        // 绘制列表标题
        reorderableList.drawHeaderCallback = (Rect rect) =>
        {
            GUI.Label(rect, "常驻内存的AB包列表");
        };

        // 加号回调
        reorderableList.onAddCallback = (ReorderableList list) =>
        {
            script.BundleNameList.Add(new BundleElement(""));
            list.index = script.BundleNameList.Count - 1;         // 选中最后一个
        };

        // 减号回调
        reorderableList.onRemoveCallback = (ReorderableList list) =>
        {
            if (EditorUtility.DisplayDialog("警告", "是否真的要删除" + script.BundleNameList[list.index].bundleName + "?", "是", "否"))
            {
                ReorderableList.defaultBehaviours.DoRemoveButton(list);
            }
        };

        // 绘制列表背景
        reorderableList.drawElementBackgroundCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
        {
            if (index == -1)
            {
                return;
            }
            bool   repeat = false;                                                      // 属性key值是否重复
            string value  = script.BundleNameList[index].bundleName;
            for (int i = 0; i < script.BundleNameList.Count; i++)
            {
                if ((i != index && value.Equals(script.BundleNameList[i].bundleName)) || (AssetBundlePermanentAsset.ERROR_TIPS == value))
                {
                    repeat = true;
                }
            }

            if (repeat)
            {
                GUI.color = new Color(1f, 0f, 0f, 0.8f);                                // 修改GUI颜色
            }
            else
            {
                GUI.color = (Color)elementDefaultColor;                                 // 还原GUI颜色
            }
            ReorderableList.defaultBehaviours.DrawElementBackground(rect, index, isActive, isFocused, true);
        };

        reorderableList.DoLayoutList();
        serializedObject.ApplyModifiedProperties();

        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }