示例#1
0
        /// <summary>
        /// 设置assetbundleName
        /// </summary>
        /// <param name="s"></param>
        public static void SetAssetBundleName(Object s, bool replace = false, bool variant = false, string variantName = null)
        {
            string abPath = AssetDatabase.GetAssetPath(s);
            AssetImporter
                   import  = AssetImporter.GetAtPath(abPath);
            string folder  = EditorUtils.GetLabelsByPath(abPath);
            string objName = s.name.ToLower();

            if (replace)
            {
                objName = HugulaEditorSetting.instance.GetAssetBundleNameByReplaceIgnore(objName);
                // Debug.LogFormat ("{0} replace {1}", s.name, objName);
            }

            bool   isScene = abPath.EndsWith(".unity");
            string name    = CUtils.GetRightFileName(objName);

            var suffix = Common.CHECK_ASSETBUNDLE_SUFFIX;

            // if (variant) // variant
            // {
            //     suffix = "";
            // }

            if (string.IsNullOrEmpty(folder))
            {
                import.assetBundleName = name + suffix;
            }
            else
            {
                import.assetBundleName = string.Format("{0}/{1}{2}", folder, name, suffix);
            }

            if (variant)
            {
                string m_variant;
                if (abPath.IndexOf("Assets/TapEnjoy/WarX/Effect/Prefabs/high") == 0)
                {
                    m_variant = "high";
                }
                else if (abPath.IndexOf("Assets/TapEnjoy/WarX/Effect/Prefabs/medium") == 0)
                {
                    m_variant = "medium";
                }
                else if (abPath.IndexOf("Assets/TapEnjoy/WarX/Effect/Prefabs/low") == 0)
                {
                    m_variant = "low";
                }
                else
                {
                    DirectoryInfo filePath = new DirectoryInfo(abPath);
                    m_variant = variantName != null ? variantName : filePath.Parent.Name.ToLower();
                }

                import.assetBundleVariant = m_variant;
                HugulaSetting.instance.AddVariant(m_variant);
                if (m_variant.Equals(Common.ASSETBUNDLE_SUFFIX))
                {
                    Debug.LogWarningFormat("{0} variant folder name Equals {1}", abPath, Common.ASSETBUNDLE_SUFFIX);
                }
                if (m_variant == Common.DOT_BYTES.Replace(".", ""))
                {
                    Debug.LogWarningFormat("{0} variant folder name Equals {1}", abPath, Common.DOT_BYTES);
                }
            }

            if (s.name.Contains(" "))
            {
                Debug.LogWarning(s.name + " contains space");
            }

            Debug.Log(import.assetBundleName + ",variant=" + import.assetBundleVariant);

            if (s is GameObject)
            {
                GameObject tar = s as GameObject;
                if (tar.transform.parent == null)
                {
                    ReferenceCount refe = LuaHelper.AddComponent(tar, typeof(ReferenceCount)) as ReferenceCount;
                    if (refe != null)
                    {
                        if (string.IsNullOrEmpty(import.assetBundleVariant))
                        {
                            refe.assetbundle = import.assetBundleName;
                        }
                        else
                        {
                            refe.assetbundle = import.assetBundleName + "." + import.assetBundleVariant;
                        }

                        EditorUtility.SetDirty(s);
                    }
                }
            }
            else if (isScene) //如果是场景需要添加引用计数脚本
            {                 //UnityEngine.SceneAsset
                var sce = s;  // as SceneAsset;
                Debug.Log(sce);
                AssetDatabase.OpenAsset(sce);
                GameObject gobj = GameObject.Find(sce.name);
                if (gobj == null)
                {
                    gobj = new GameObject(sce.name);
                }
                ReferenceCount refe = LuaHelper.AddComponent(gobj, typeof(ReferenceCount)) as ReferenceCount;
                if (refe != null)
                {
                    if (string.IsNullOrEmpty(import.assetBundleVariant))
                    {
                        refe.assetbundle = import.assetBundleName;
                    }
                    else
                    {
                        refe.assetbundle = import.assetBundleName + "." + import.assetBundleVariant;
                    }

                    EditorUtility.SetDirty(sce);
                }

                var refers = GameObject.FindObjectsOfType <ReferenceCount> ();
                foreach (var rf in refers)
                {
                    if (rf != refe)
                    {
                        Debug.LogWarningFormat("you should not add ReferenceCount in {0}", EditorUtils.GetGameObjectPathInScene(rf.transform, string.Empty));
                    }
                }
            }
        }