コード例 #1
0
ファイル: BuildScript.cs プロジェクト: xiangaodielian/xasset
        /// 传入manifestCache表示外部负责文件保存,避免 loop 中多次调用AssetDatabase.SaveAssets影响速度
        public static void SetAssetBundleNameAndVariant(string assetPath, string bundleName, string variant, AssetsManifest manifestCache = null)
        {
            var manifest = manifestCache == null?GetManifest() : manifestCache;

            var dir      = Path.GetDirectoryName(assetPath).Replace("\\", "/");
            var dirs     = manifest.dirs;
            var dirIndex = ArrayUtility.FindIndex(dirs, (string obj) => { return(obj == dir); });

            if (dirIndex == -1)
            {
                ArrayUtility.Add(ref manifest.dirs, dir);
                dirIndex = manifest.dirs.Length - 1;
                dirs     = manifest.dirs;
            }

            var assetBundleNames = manifest.bundles;
            var bundleIndex      = ArrayUtility.FindIndex(assetBundleNames, (string obj) => { return(obj == bundleName); });

            if (bundleIndex == -1)
            {
                ArrayUtility.Add(ref manifest.bundles, bundleName);
                assetBundleNames = manifest.bundles;
                bundleIndex      = assetBundleNames.Length - 1;
            }

            var variantNames = manifest.activeVariants;
            var variantIndex = ArrayUtility.FindIndex(variantNames, (string obj) => { return(obj == variant); });

            if (variantIndex == -1 && !string.IsNullOrEmpty(variant))
            {
                ArrayUtility.Add(ref manifest.activeVariants, variant);
                variantNames = manifest.activeVariants;
                variantIndex = variantNames.Length - 1;
            }

            var assets     = manifest.assets;
            var assetIndex = ArrayUtility.FindIndex(assets, (AssetData obj) =>
            {
                var path = dirs[obj.dir] + "/" + obj.name;
                return(path == assetPath);
            });

            if (assetIndex == -1)
            {
                var info = new AssetData();
                ArrayUtility.Add(ref manifest.assets, info);
                assetIndex = manifest.assets.Length - 1;
                assets     = manifest.assets;
            }

            var asset = assets[assetIndex];

            asset.name    = Path.GetFileName(assetPath);
            asset.bundle  = bundleIndex;
            asset.variant = variantIndex;
            asset.dir     = dirIndex;
            if (manifestCache == null)
            {
                EditorUtility.SetDirty(manifest);
                AssetDatabase.SaveAssets();
            }
        }
コード例 #2
0
        public static bool _set(Font[] fontList)
        {
            var EditorStylesType = Assembly.Load("UnityEditor.dll").GetType("UnityEditor.EditorStyles");
            var s_Current        = EditorStylesType.GetField("s_Current", BindingFlags.NonPublic | BindingFlags.Static);
            var s_CurrentV       = s_Current.GetValue(null);

            if (s_CurrentV == null)
            {
                return(false);
            }

            var EditorStylesFields = EditorStylesType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (var p in EditorStylesFields)
            {
                if (typeof(GUIStyle) == p.FieldType)
                {
                    var style = p.GetValue(s_CurrentV) as GUIStyle;
                    if (style.font == null)
                    {
                    }
                    else
                    {
                        //Lucida Grande Big
                        //Lucida Grande Warning
                        if (s_Apply)
                        {
                            if (s_fontBak.ContainsKey(p.Name))
                            {
                                var idx = ArrayUtility.FindIndex(LucidaGrande, a => a == s_fontBak[p.Name]);
                                style.font = fontList[idx];
                                //Debug.Log( "revert: " + p.Name +" : "+ s_fontBak[ p.Name ] );
                            }
                        }
                        else
                        {
                            for (int i = 0; i < fontList.Length; i++)
                            {
                                if (style.font.name == LucidaGrande[i] && fontList[i] != null)
                                {
                                    style.font = fontList[i];
                                    s_fontBak.Add(p.Name, LucidaGrande[i]);
                                }
                            }
                        }
                    }
                }
                if (typeof(Font) == p.FieldType)
                {
                    if (p.Name == "m_StandardFont")
                    {
                        if (fontList.StandardFont() != null)
                        {
                            p.SetValue(s_CurrentV, fontList.StandardFont());
                        }
                    }
                    if (p.Name == "m_BoldFont")
                    {
                        if (fontList.BoldFont() != null)
                        {
                            p.SetValue(s_CurrentV, fontList.BoldFont());
                        }
                    }
                    if (p.Name == "m_MiniFont")
                    {
                        if (fontList.MiniFont() != null)
                        {
                            p.SetValue(s_CurrentV, fontList.MiniFont());
                        }
                    }
                    if (p.Name == "m_MiniBoldFont")
                    {
                        if (fontList.MiniBoldFont() != null)
                        {
                            p.SetValue(s_CurrentV, fontList.MiniBoldFont());
                        }
                    }
                }
            }

            //foreach( var e in Resources.FindObjectsOfTypeAll( typeof( EditorWindow ) ) ) {
            //	if( e == null ) continue;
            //	( (EditorWindow) e ).Repaint();
            //}
            EditorUtils.RepaintEditorWindow();

            return(true);
        }
コード例 #3
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            {
                var ext = Path.GetExtension(ctx.assetPath).ToLower();
                if (ext == ".vox")
                {
                    fileType = VoxelBase.FileType.vox;
                }
                else if (ext == ".qb")
                {
                    fileType = VoxelBase.FileType.qb;
                }
                else
                {
                    return;
                }
            }

            #region DefaultScale
            if (dataVersion == 0 &&
                importScale == Vector3.one &&
                AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(ctx.assetPath) == null)
            {
                var x = EditorPrefs.GetFloat("VoxelImporter_DefaultScaleX", 1f);
                var y = EditorPrefs.GetFloat("VoxelImporter_DefaultScaleY", 1f);
                var z = EditorPrefs.GetFloat("VoxelImporter_DefaultScaleZ", 1f);
                importScale = new Vector3(x, y, z);
            }
            #endregion

            Action <string> LogImportError = (log) =>
            {
#if UNITY_2018_1_OR_NEWER
                ctx.LogImportError(log);
#else
                Debug.LogError(log);
#endif
            };

            var gameObject  = new GameObject(Path.GetFileNameWithoutExtension(ctx.assetPath));
            var voxelObject = gameObject.AddComponent <VoxelObject>();
            {
                voxelObject.legacyVoxImport               = legacyVoxImport;
                voxelObject.importMode                    = importMode;
                voxelObject.importScale                   = importScale;
                voxelObject.importOffset                  = importOffset;
                voxelObject.combineFaces                  = combineFaces;
                voxelObject.ignoreCavity                  = ignoreCavity;
                voxelObject.voxelStructure                = outputStructure ? ScriptableObject.CreateInstance <VoxelStructure>() : null;
                voxelObject.generateLightmapUVs           = generateLightmapUVs;
                voxelObject.generateLightmapUVsAngleError = generateLightmapUVsAngleError;
                voxelObject.generateLightmapUVsAreaError  = generateLightmapUVsAreaError;
                voxelObject.generateLightmapUVsHardAngle  = generateLightmapUVsHardAngle;
                voxelObject.generateLightmapUVsPackMargin = generateLightmapUVsPackMargin;
                voxelObject.generateTangents              = generateTangents;
                voxelObject.meshFaceVertexOffset          = meshFaceVertexOffset;
                voxelObject.loadFromVoxelFile             = loadFromVoxelFile;
                voxelObject.generateMipMaps               = generateMipMaps;
            }
            var objectCore = new VoxelObjectCore(voxelObject);
            try
            {
                if (!objectCore.Create(ctx.assetPath, null))
                {
                    LogImportError(string.Format("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath));
                    DestroyImmediate(gameObject);
                    return;
                }
            }
            catch
            {
                LogImportError(string.Format("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath));
                DestroyImmediate(gameObject);
                return;
            }

            #region Correspondence in Issue ID 947055 Correction in case before correction is applied
            for (int i = 0; i < voxelObject.materials.Count; i++)
            {
                if (voxelObject.materials[i] != null)
                {
                    voxelObject.materials[i].hideFlags |= HideFlags.NotEditable;
                }
            }
            if (voxelObject.atlasTexture != null)
            {
                voxelObject.atlasTexture.hideFlags |= HideFlags.NotEditable;
            }
            if (voxelObject.mesh != null)
            {
                voxelObject.mesh.hideFlags |= HideFlags.NotEditable;
            }
            #endregion

            #region Material
            {
                materials     = new Material[voxelObject.materialIndexes.Count];
                materialNames = new string[voxelObject.materialIndexes.Count];
                for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                {
                    var index    = voxelObject.materialIndexes[i];
                    var material = voxelObject.materials[index];
                    material.name    = string.Format("mat{0}", index);
                    materials[i]     = material;
                    materialNames[i] = material.name;
                }
                if (remappedMaterials != null)
                {
                    remappedMaterials = remappedMaterials.Where(item => item.material != null).ToArray();
                }
            }
            #endregion

            #region Collider
            switch (colliderType)
            {
            case ColliderType.Box:
                gameObject.AddComponent <BoxCollider>();
                break;

            case ColliderType.Sphere:
                gameObject.AddComponent <SphereCollider>();
                break;

            case ColliderType.Capsule:
                gameObject.AddComponent <CapsuleCollider>();
                break;

            case ColliderType.Mesh:
                gameObject.AddComponent <MeshCollider>();
                break;
            }
            #endregion

#if UNITY_2017_3_OR_NEWER
            ctx.AddObjectToAsset(gameObject.name, gameObject);
            ctx.AddObjectToAsset(voxelObject.mesh.name = "mesh", voxelObject.mesh);
            {
                var materials = new List <Material>();
                for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                {
                    var material = voxelObject.materials[voxelObject.materialIndexes[i]];
                    materials.Add(material);
                    if (remappedMaterials != null)
                    {
                        var index = ArrayUtility.FindIndex(remappedMaterials, (t) => { return(t.name == material.name); });
                        if (index >= 0)
                        {
                            materials[i] = remappedMaterials[index].material;
                            continue;
                        }
                    }
                    ctx.AddObjectToAsset(material.name, material);
                }
                gameObject.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();
            }
            ctx.AddObjectToAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
            if (voxelObject.voxelStructure != null)
            {
                ctx.AddObjectToAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
            }

            VoxelObject.DestroyImmediate(voxelObject);

            ctx.SetMainObject(gameObject);
#else
            ctx.SetMainAsset(gameObject.name, gameObject);
            ctx.AddSubAsset(voxelObject.mesh.name = "mesh", voxelObject.mesh);
            for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
            {
                var material = voxelObject.materials[voxelObject.materialIndexes[i]];
                ctx.AddSubAsset(material.name, material);
            }
            ctx.AddSubAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
            if (voxelObject.voxelStructure != null)
            {
                ctx.AddSubAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
            }

            VoxelObject.DestroyImmediate(voxelObject);
#endif
            dataVersion = EditorDataVersion;
        }
コード例 #4
0
    void OnGUI()
    {
        // Description
        Rect descRect = EditorGUILayout.BeginVertical();

        EditorGUI.DrawRect(descRect, light);
        if (!string.IsNullOrEmpty(description))
        {
            GUIStyle multiLineBold = new GUIStyle();
            multiLineBold.font     = EditorStyles.boldFont;
            multiLineBold.wordWrap = true;
            multiLineBold.padding  = new RectOffset(8, 8, 16, 16);
            EditorGUILayout.LabelField(description, multiLineBold);
        }
        EditorGUI.DrawRect(new Rect(descRect.x, descRect.yMax, descRect.width, 1f), Color.grey);
        EditorGUILayout.EndVertical();

        // Selection
        if (selectionRoot.children.Count == 0)
        {
            EditorGUILayout.HelpBox("Nothing selected", MessageType.Info);
            GUILayout.FlexibleSpace();
        }
        else
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            foreach (AssetItem asset in selectionRoot.children)
            {
                DoItemGUI(asset);
            }
            EditorGUILayout.EndScrollView();
        }

        // DragAndDrop operation
        if (Event.current.type == EventType.DragUpdated)
        {
            Object[] objs = DragAndDrop.objectReferences;
            if (ArrayUtility.FindIndex(objs, (obj) => AssetDatabase.IsMainAsset(obj)) >= 0)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Link;
            }
        }
        else if (Event.current.type == EventType.DragPerform)
        {
            DragAndDrop.AcceptDrag();
            Object[] objs = DragAndDrop.objectReferences;
            foreach (Object obj in objs)
            {
                string path = AssetDatabase.GetAssetPath(obj);
                SortedInsert(path, true);
            }
            ValidateSelection();
        }

        // Options
        Rect optionsRect = EditorGUILayout.BeginVertical();

        EditorGUI.DrawRect(optionsRect, light);
        EditorGUI.DrawRect(new Rect(optionsRect.x, optionsRect.yMin, optionsRect.width, 1f), lighter);
        EditorGUILayout.Space();
        if (OnOptionsGUI != null)
        {
            OnOptionsGUI(this);
        }
        else
        {
            GUILayout.Label("Please, reopen this window to reload the options", EditorStyles.centeredGreyMiniLabel);
        }
        EditorGUILayout.Space();
        EditorGUILayout.EndVertical();
    }
コード例 #5
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            {
                var ext = Path.GetExtension(ctx.assetPath).ToLower();
                if (ext == ".vox")
                {
                    fileType = VoxelBase.FileType.vox;
                }
                else if (ext == ".qb")
                {
                    fileType = VoxelBase.FileType.qb;
                }
                else
                {
                    return;
                }
            }

            #region DefaultScale
            if (dataVersion == 0 &&
                importScale == Vector3.one &&
                AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(ctx.assetPath) == null)
            {
                var x = EditorPrefs.GetFloat("VoxelImporter_DefaultScaleX", 1f);
                var y = EditorPrefs.GetFloat("VoxelImporter_DefaultScaleY", 1f);
                var z = EditorPrefs.GetFloat("VoxelImporter_DefaultScaleZ", 1f);
                importScale = new Vector3(x, y, z);
            }
            #endregion

            Action <string> LogImportError = (log) =>
            {
#if UNITY_2018_1_OR_NEWER
                ctx.LogImportError(log);
#else
                Debug.LogError(log);
#endif
            };
            Action <VoxelBase> SetBasicOptions = (voxelObject) =>
            {
                voxelObject.legacyVoxImport               = legacyVoxImport;
                voxelObject.importMode                    = importMode;
                voxelObject.importScale                   = importScale;
                voxelObject.importOffset                  = importOffset;
                voxelObject.combineFaces                  = combineFaces;
                voxelObject.ignoreCavity                  = ignoreCavity;
                voxelObject.shareSameFace                 = shareSameFace;
                voxelObject.removeUnusedPalettes          = removeUnusedPalettes;
                voxelObject.voxelStructure                = outputStructure ? ScriptableObject.CreateInstance <VoxelStructure>() : null;
                voxelObject.generateLightmapUVs           = generateLightmapUVs;
                voxelObject.generateLightmapUVsAngleError = generateLightmapUVsAngleError;
                voxelObject.generateLightmapUVsAreaError  = generateLightmapUVsAreaError;
                voxelObject.generateLightmapUVsHardAngle  = generateLightmapUVsHardAngle;
                voxelObject.generateLightmapUVsPackMargin = generateLightmapUVsPackMargin;
                voxelObject.generateTangents              = generateTangents;
                voxelObject.meshFaceVertexOffset          = meshFaceVertexOffset;
                voxelObject.loadFromVoxelFile             = loadFromVoxelFile;
                voxelObject.generateMipMaps               = generateMipMaps;
            };

            Action <VoxelBaseCore> Export = (core) =>
            {
                if (export)
                {
                    var fullPath = Application.dataPath + ctx.assetPath.Remove(0, "Assets".Length);
                    fullPath = fullPath.Remove(fullPath.LastIndexOf('.')) + ".dae";
                    core.ExportDaeFile(fullPath, false);
                }
            };

            var gameObject = new GameObject(Path.GetFileNameWithoutExtension(ctx.assetPath));
            if (string.IsNullOrEmpty(gameObjectName))
            {
                gameObjectName = gameObject.name;
            }
            if (meshMode == MeshMode.Combine)
            {
                #region Combine
                var voxelObject = gameObject.AddComponent <VoxelObject>();
                SetBasicOptions(voxelObject);
                var objectCore = new VoxelObjectCore(voxelObject);
                try
                {
                    if (!objectCore.Create(ctx.assetPath, null))
                    {
                        LogImportError(string.Format("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath));
                        DestroyImmediate(gameObject);
                        return;
                    }
                }
                catch
                {
                    LogImportError(string.Format("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath));
                    DestroyImmediate(gameObject);
                    return;
                }

                #region Correspondence in Issue ID 947055 Correction in case before correction is applied
                foreach (var material in voxelObject.materials)
                {
                    if (material != null)
                    {
                        material.hideFlags |= HideFlags.NotEditable;
                    }
                }
                if (voxelObject.atlasTexture != null)
                {
                    voxelObject.atlasTexture.hideFlags |= HideFlags.NotEditable;
                }
                if (voxelObject.mesh != null)
                {
                    voxelObject.mesh.hideFlags |= HideFlags.NotEditable;
                }
                #endregion

                #region Material
                {
                    materials     = new Material[voxelObject.materialIndexes.Count];
                    materialNames = new string[voxelObject.materialIndexes.Count];
                    for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                    {
                        var index    = voxelObject.materialIndexes[i];
                        var material = voxelObject.materials[index];
                        material.name    = string.Format("mat{0}", index);
                        materials[i]     = material;
                        materialNames[i] = material.name;
                    }
                    if (remappedMaterials != null)
                    {
                        remappedMaterials = remappedMaterials.Where(item => item.material != null && materialNames.Contains(item.name)).ToArray();
                    }
                }
                #endregion

                #region Collider
                switch (colliderType)
                {
                case ColliderType.Box:
                    gameObject.AddComponent <BoxCollider>();
                    break;

                case ColliderType.Sphere:
                    gameObject.AddComponent <SphereCollider>();
                    break;

                case ColliderType.Capsule:
                    gameObject.AddComponent <CapsuleCollider>();
                    break;

                case ColliderType.Mesh:
                    gameObject.AddComponent <MeshCollider>();
                    break;
                }
                #endregion

                Export(objectCore);

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(gameObjectName, gameObject);
                ctx.AddObjectToAsset(voxelObject.mesh.name = "mesh", voxelObject.mesh);
                {
                    var list = new List <Material>();
                    for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                    {
                        var material = voxelObject.materials[voxelObject.materialIndexes[i]];
                        list.Add(material);
                        if (remappedMaterials != null)
                        {
                            var index = ArrayUtility.FindIndex(remappedMaterials, (t) => { return(t.name == material.name); });
                            if (index >= 0)
                            {
                                list[i] = remappedMaterials[index].material;
                                continue;
                            }
                        }
                        ctx.AddObjectToAsset(material.name, material);
                    }
                    gameObject.GetComponent <MeshRenderer>().sharedMaterials = list.ToArray();
                }
                ctx.AddObjectToAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
                if (voxelObject.voxelStructure != null)
                {
                    ctx.AddObjectToAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
                }

                VoxelObject.DestroyImmediate(voxelObject);

                ctx.SetMainObject(gameObject);
#else
                ctx.SetMainAsset(gameObjectName, gameObject);
                ctx.AddSubAsset(voxelObject.mesh.name = "mesh", voxelObject.mesh);
                for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                {
                    var material = voxelObject.materials[voxelObject.materialIndexes[i]];
                    ctx.AddSubAsset(material.name, material);
                }
                ctx.AddSubAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
                if (voxelObject.voxelStructure != null)
                {
                    ctx.AddSubAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
                }

                VoxelObject.DestroyImmediate(voxelObject);
#endif
                #endregion
            }
            else if (meshMode == MeshMode.Individual)
            {
                #region Individual
                var voxelObject = gameObject.AddComponent <VoxelChunksObject>();
                SetBasicOptions(voxelObject);
                {
                    voxelObject.createContactChunkFaces = createContactChunkFaces;
                    voxelObject.materialMode            = materialMode;
                }
                var objectCore = new VoxelChunksObjectCore(voxelObject);
                {
                    objectCore.chunkNameFormat = "{0}";
                }
                try
                {
                    if (!objectCore.Create(ctx.assetPath, null))
                    {
                        LogImportError(string.Format("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath));
                        DestroyImmediate(gameObject);
                        return;
                    }
                }
                catch
                {
                    LogImportError(string.Format("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath));
                    DestroyImmediate(gameObject);
                    return;
                }

                #region Correspondence in Issue ID 947055 Correction in case before correction is applied
                if (voxelObject.materials != null)
                {
                    foreach (var material in voxelObject.materials)
                    {
                        if (material != null)
                        {
                            material.hideFlags |= HideFlags.NotEditable;
                        }
                    }
                }
                if (voxelObject.atlasTexture != null)
                {
                    voxelObject.atlasTexture.hideFlags |= HideFlags.NotEditable;
                }
                foreach (var chunk in voxelObject.chunks)
                {
                    if (chunk.materials != null)
                    {
                        foreach (var material in chunk.materials)
                        {
                            if (material != null)
                            {
                                material.hideFlags |= HideFlags.NotEditable;
                            }
                        }
                    }
                    if (chunk.atlasTexture != null)
                    {
                        chunk.atlasTexture.hideFlags |= HideFlags.NotEditable;
                    }
                    if (chunk.mesh != null)
                    {
                        chunk.mesh.hideFlags |= HideFlags.NotEditable;
                    }
                }
                #endregion

                #region Material
                {
                    if (materialMode == VoxelChunksObject.MaterialMode.Combine)
                    {
                        materials     = new Material[voxelObject.materialIndexes.Count];
                        materialNames = new string[voxelObject.materialIndexes.Count];
                        for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                        {
                            var index    = voxelObject.materialIndexes[i];
                            var material = voxelObject.materials[index];
                            material.name    = string.Format("mat{0}", index);
                            materials[i]     = material;
                            materialNames[i] = material.name;
                        }
                    }
                    else if (materialMode == VoxelChunksObject.MaterialMode.Individual)
                    {
                        List <Material> list = new List <Material>();
                        foreach (var chunk in voxelObject.chunks)
                        {
                            for (int i = 0; i < chunk.materialIndexes.Count; i++)
                            {
                                var index    = chunk.materialIndexes[i];
                                var material = chunk.materials[index];
                                material.name = chunk.gameObject.name + string.Format("_mat{0}", index);
                                if (!list.Contains(material))
                                {
                                    list.Add(material);
                                }
                            }
                        }
                        materials     = list.ToArray();
                        materialNames = new string[list.Count];
                        for (int i = 0; i < list.Count; i++)
                        {
                            materialNames[i] = list[i].name;
                        }
                    }
                    if (remappedMaterials != null)
                    {
                        remappedMaterials = remappedMaterials.Where(item => item.material != null && materialNames.Contains(item.name)).ToArray();
                    }
                }
                #endregion

                #region Collider
                foreach (var chunk in voxelObject.chunks)
                {
                    switch (colliderType)
                    {
                    case ColliderType.Box:
                        chunk.gameObject.AddComponent <BoxCollider>();
                        break;

                    case ColliderType.Sphere:
                        chunk.gameObject.AddComponent <SphereCollider>();
                        break;

                    case ColliderType.Capsule:
                        chunk.gameObject.AddComponent <CapsuleCollider>();
                        break;

                    case ColliderType.Mesh:
                        chunk.gameObject.AddComponent <MeshCollider>();
                        break;
                    }
                }
                #endregion

                Export(objectCore);

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(gameObjectName, gameObject);
                foreach (var chunk in voxelObject.chunks)
                {
                    ctx.AddObjectToAsset(chunk.mesh.name = chunk.gameObject.name + "_mesh", chunk.mesh);
                }
                {
                    if (materialMode == VoxelChunksObject.MaterialMode.Combine)
                    {
                        var materials = new List <Material>();
                        for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                        {
                            var material = voxelObject.materials[voxelObject.materialIndexes[i]];
                            materials.Add(material);
                            if (remappedMaterials != null)
                            {
                                var index = ArrayUtility.FindIndex(remappedMaterials, (t) => { return(t.name == material.name); });
                                if (index >= 0)
                                {
                                    materials[i] = remappedMaterials[index].material;
                                    continue;
                                }
                            }
                            ctx.AddObjectToAsset(material.name, material);
                        }
                        foreach (var chunk in voxelObject.chunks)
                        {
                            chunk.gameObject.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();
                        }
                        ctx.AddObjectToAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
                    }
                    else if (materialMode == VoxelChunksObject.MaterialMode.Individual)
                    {
                        foreach (var chunk in voxelObject.chunks)
                        {
                            var materials = new List <Material>();
                            for (int i = 0; i < chunk.materialIndexes.Count; i++)
                            {
                                var material = chunk.materials[chunk.materialIndexes[i]];
                                materials.Add(material);
                                if (remappedMaterials != null)
                                {
                                    var index = ArrayUtility.FindIndex(remappedMaterials, (t) => { return(t.name == material.name); });
                                    if (index >= 0)
                                    {
                                        materials[i] = remappedMaterials[index].material;
                                        continue;
                                    }
                                }
                                ctx.AddObjectToAsset(material.name, material);
                            }
                            chunk.gameObject.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();
                            ctx.AddObjectToAsset(chunk.atlasTexture.name = chunk.gameObject.name + "_tex", chunk.atlasTexture);
                        }
                    }
                }
                if (voxelObject.voxelStructure != null)
                {
                    ctx.AddObjectToAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
                }

                foreach (var chunk in voxelObject.chunks)
                {
                    VoxelChunksObjectChunk.DestroyImmediate(chunk.GetComponent <VoxelChunksObjectChunk>());
                }
                VoxelObject.DestroyImmediate(voxelObject);

                ctx.SetMainObject(gameObject);
#else
                ctx.SetMainAsset(gameObjectName, gameObject);
                foreach (var chunk in voxelObject.chunks)
                {
                    ctx.AddSubAsset(chunk.mesh.name = chunk.gameObject.name + "_mesh", chunk.mesh);
                }
                {
                    if (materialMode == VoxelChunksObject.MaterialMode.Combine)
                    {
                        var materials = new List <Material>();
                        for (int i = 0; i < voxelObject.materialIndexes.Count; i++)
                        {
                            var material = voxelObject.materials[voxelObject.materialIndexes[i]];
                            materials.Add(material);
                            if (remappedMaterials != null)
                            {
                                var index = ArrayUtility.FindIndex(remappedMaterials, (t) => { return(t.name == material.name); });
                                if (index >= 0)
                                {
                                    materials[i] = remappedMaterials[index].material;
                                    continue;
                                }
                            }
                            ctx.AddSubAsset(material.name, material);
                        }
                        foreach (var chunk in voxelObject.chunks)
                        {
                            chunk.gameObject.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();
                        }
                        ctx.AddSubAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
                    }
                    else if (materialMode == VoxelChunksObject.MaterialMode.Individual)
                    {
                        foreach (var chunk in voxelObject.chunks)
                        {
                            var materials = new List <Material>();
                            for (int i = 0; i < chunk.materialIndexes.Count; i++)
                            {
                                var material = chunk.materials[chunk.materialIndexes[i]];
                                materials.Add(material);
                                if (remappedMaterials != null)
                                {
                                    var index = ArrayUtility.FindIndex(remappedMaterials, (t) => { return(t.name == material.name); });
                                    if (index >= 0)
                                    {
                                        materials[i] = remappedMaterials[index].material;
                                        continue;
                                    }
                                }
                                ctx.AddSubAsset(material.name, material);
                            }
                            chunk.gameObject.GetComponent <MeshRenderer>().sharedMaterials = materials.ToArray();
                            ctx.AddSubAsset(chunk.atlasTexture.name = chunk.gameObject.name + "_tex", chunk.atlasTexture);
                        }
                    }
                }
                if (voxelObject.voxelStructure != null)
                {
                    ctx.AddSubAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
                }

                foreach (var chunk in voxelObject.chunks)
                {
                    VoxelChunksObjectChunk.DestroyImmediate(chunk.GetComponent <VoxelChunksObjectChunk>());
                }
                VoxelObject.DestroyImmediate(voxelObject);
#endif
                #endregion
            }

            dataVersion = EditorDataVersion;
        }
コード例 #6
0
    private void ThreadRenderBackupFont(int backupLevel, int xOffsetDist)
    {
        if (m_FontBackupPaths == null || m_FontBackupPaths.Length <= backupLevel)
        {
            return;
        }

        List <int> list = new List <int>();

        for (int index = 0; index < m_CharacterCount; ++index)
        {
            if (m_FontGlyphInfo[index].x == -1)
            {
                list.Add(m_FontGlyphInfo[index].id);
            }
        }

        // 如果有指定字体,则在这里插入
        if (m_CharacterUseFontBackup != null && m_CharacterUseFontBackup.Length > backupLevel)
        {
            foreach (var character in m_CharacterUseFontBackup[backupLevel])
            {
                if (!string.IsNullOrEmpty(character))
                {
                    for (int i = 0; i < character.Length; i++)
                    {
                        // Check to make sure we don't include duplicates
                        if (list.FindIndex(item => item == character[i]) == -1)
                        {
                            list.Add(character[i]);
                        }
                    }
                }
            }
        }

        if (list.Count == 0)
        {
            return;
        }

        int[]  characterSet = list.ToArray();
        string fontPath     = m_FontBackupPaths[backupLevel];
        int    errorCode    = TMPro_FontPlugin.Load_TrueType_Font(fontPath);

        if (errorCode != 0)
        {
            return;
        }

        var tmpAtlasWidth     = 512;
        var tmpAtlasHeight    = 512;
        var tmpTextureBuffer  = new byte[tmpAtlasWidth * tmpAtlasHeight];
        var tmpCharacterCount = characterSet.Length;
        var tmpFontFaceInfo   = default(FT_FaceInfo);
        var tmpFontGlyphInfo  = new FT_GlyphInfo[tmpCharacterCount];

        bool  autoSizing = m_PointSizeSamplingMode == 0;
        float strokeSize = m_FontStyleValue;

        if (m_RenderMode == RenderModes.DistanceField16)
        {
            strokeSize = m_FontStyleValue * 16;
        }
        if (m_RenderMode == RenderModes.DistanceField32)
        {
            strokeSize = m_FontStyleValue * 32;
        }

        errorCode = TMPro_FontPlugin.Render_Characters(tmpTextureBuffer, tmpAtlasWidth,
                                                       tmpAtlasHeight, m_Padding, characterSet, tmpCharacterCount, m_FontStyle, strokeSize,
                                                       autoSizing, m_RenderMode, (int)m_PackingMode, ref tmpFontFaceInfo, tmpFontGlyphInfo);
        if (errorCode != 0)
        {
            return;
        }

        int wordWidth = m_PointSize;
        int xStart    = xOffsetDist - m_Padding * 2 - wordWidth; // 从padding开始拷贝,否则会出现负偏移丢失的情况
        int yStart    = m_AtlasHeight - m_Padding - 1;
        int numY      = 0;

        for (int index = 0; index < tmpCharacterCount; ++index)
        {
            if (!Mathf.Approximately(tmpFontGlyphInfo[index].x, -1))
            {
                var gi = tmpFontGlyphInfo[index];
                var x  = Mathf.FloorToInt(gi.x) - m_Padding;
                var y  = tmpAtlasHeight - (Mathf.FloorToInt(gi.y) - m_Padding);
                var w  = Mathf.CeilToInt(gi.width) + m_Padding * 2;
                var h  = Mathf.CeilToInt(gi.height) + m_Padding * 2;

                for (int r = 0; r < h; r++)
                {
                    for (int c = 0; c < w; c++)
                    {
                        m_TextureBuffer[(yStart - r) * m_AtlasWidth + c + xStart] =
                            tmpTextureBuffer[(y - r) * tmpAtlasWidth + c + x];
                    }
                }
                var idx = ArrayUtility.FindIndex(m_FontGlyphInfo, info => info.id == gi.id);
                if (idx == -1)
                {
                    // 往数组里面添加
                    ArrayUtility.Add(ref m_FontGlyphInfo, new FT_GlyphInfo()
                    {
                        id = gi.id
                    });
                    idx = m_FontGlyphInfo.Length - 1;
                }

                var gi2 = m_FontGlyphInfo[idx];
                gi2.x                = xStart + m_Padding;
                gi2.y                = m_AtlasHeight - yStart + m_Padding;
                gi2.width            = gi.width;
                gi2.height           = gi.height;
                gi2.xAdvance         = gi.xAdvance;
                gi2.xOffset          = gi.xOffset;
                gi2.yOffset          = gi.yOffset;
                m_FontGlyphInfo[idx] = gi2;

                yStart = yStart - h - m_Padding - 1;
                numY++;

                // 如果超过五个则换一列
                if (numY > 5)
                {
                    numY   = 0;
                    xStart = xStart - m_Padding * 2 - wordWidth;
                    yStart = m_AtlasHeight - m_Padding - 1;
                }
            }
        }

        ThreadRenderBackupFont(++backupLevel, xStart);
    }
 int FindIndex(GUISkin skin, string name)
 {
     return(ArrayUtility.FindIndex(skin.customStyles, s => s.name == name.ToString()));
 }