コード例 #1
0
        public static SVGDocumentAsset CreateInstance(string svgFile, SVGError[] errors = null, string title = null, string description = null)
        {
            SVGDocumentAsset svgDocumentAsset = ScriptableObject.CreateInstance <SVGDocumentAsset>();

            svgDocumentAsset._description = description;
            svgDocumentAsset._title       = title;
            svgDocumentAsset._svgFile     = svgFile;
            svgDocumentAsset._errors      = errors;
            return(svgDocumentAsset);
        }
コード例 #2
0
ファイル: SVGAsset.cs プロジェクト: Avatarchik/OutOfTheBox
        internal void _editor_ApplyChanges(bool importMultipleFiles = false)
        {
            if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode || UnityEditor.EditorApplication.isCompiling)
            {
                return;
            }

            if (_documentAsset != null)
            {
                _documentAsset.errors = null;
            }

            if (_sharedShaders != null)
            {
                _sharedShaders = null;
            }

            if (_sharedMesh != null)
            {
                Object.DestroyImmediate(_sharedMesh, true);
                _sharedMesh = null;
            }

            if (_atlasTextures != null && _atlasTextures.Length > 0)
            {
                for (int i = 0; i < _atlasTextures.Length; i++)
                {
                    if (_atlasTextures[i] == null)
                    {
                        continue;
                    }

                    Object.DestroyImmediate(_atlasTextures[i], true);
                    _atlasTextures[i] = null;
                }
                _atlasTextures = null;
            }

            if (_sharedUIMaterial != null)
            {
                Object.DestroyImmediate(_sharedUIMaterial, true);
                _sharedUIMaterial = null;
            }

            if (_sharedUIMaskMaterial != null)
            {
                Object.DestroyImmediate(_sharedUIMaskMaterial, true);
                _sharedUIMaskMaterial = null;
            }

            if (_sharedMaterials != null && _sharedMaterials.Length > 0)
            {
                for (int i = 0; i < sharedMaterials.Length; i++)
                {
                    if (_sharedMaterials[i] == null)
                    {
                        continue;
                    }

                    Object.DestroyImmediate(_sharedMaterials[i], true);
                    _sharedMaterials[i] = null;
                }
                _sharedMaterials = null;
            }

            string assetPath = UnityEditor.AssetDatabase.GetAssetPath(this);

            Object[] assets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(assetPath);
            if (assets != null && assets.Length > 0)
            {
                for (int i = 0; i < assets.Length; i++)
                {
                    if (assets[i] == null)
                    {
                        continue;
                    }
                    if (assets[i] == this)
                    {
                        continue;
                    }

                    if (assets[i] is SVGDocumentAsset)
                    {
                        continue;
                    }

                    DestroyImmediate(assets[i], true);
                }
            }

            _editor_LoadSVG();

            // Create Document Asset
            if (_documentAsset == null)
            {
                _documentAsset = AddObjectToAsset <SVGDocumentAsset>(ScriptableObject.CreateInstance <SVGDocumentAsset>(), this, HideFlags.HideInHierarchy);
            }

            var svgAssetPath     = UnityEditor.AssetDatabase.GetAssetPath(this);
            var svgAssetImporter = UnityEditor.AssetImporter.GetAtPath(svgAssetPath);

            if (!string.IsNullOrEmpty(svgFile))
            {
                svgAssetImporter.userData = svgFile;
            }

            if (keepSVGFile)
            {
                _documentAsset.svgFile = svgAssetImporter.userData;
            }
            else
            {
                _documentAsset.svgFile = null;
            }

            //_documentAsset.infoString = _editor_UpdateInfo();

            // Save SVG Errors in Documet Asset
            if (SVGAssetImport.errors != null && SVGAssetImport.errors.Count > 0)
            {
                _documentAsset.errors = SVGAssetImport.errors.ToArray();

                bool   critical     = false;
                string errors       = "";
                int    errorsLength = _documentAsset.errors.Length;
                for (int i = 0; i < errorsLength; i++)
                {
                    if (i < errorsLength - 1)
                    {
                        errors += _documentAsset.errors[i].ToString() + ", ";
                    }
                    else
                    {
                        errors += _documentAsset.errors[i].ToString() + ".";
                    }

                    if (_documentAsset.errors[i] == SVGError.CorruptedFile ||
                        _documentAsset.errors[i] == SVGError.Syntax)
                    {
                        critical = true;
                    }
                }

                if (critical)
                {
                    Debug.LogError("SVGAsset: " + this.name + "\nerrors: " + errors + "\npath: " + UnityEditor.AssetDatabase.GetAssetPath(this) + "\n", this);
                }
                else
                {
                    Debug.LogWarning("SVGAsset: " + this.name + "\nerrors: " + errors + "\npath: " + UnityEditor.AssetDatabase.GetAssetPath(this) + "\n", this);
                }
            }

            UnityEditor.EditorUtility.SetDirty(_documentAsset);

            // Clean old values
            _svgFile = null;

            if (SVGAssetImport.errors != null)
            {
                SVGAssetImport.errors.Clear();
                SVGAssetImport.errors = null;
            }

            if (_sharedMesh != null && _sharedMesh.vertexCount > 0)
            {
                _sharedMesh.name = this.name;

                int vertexCount = _sharedMesh.vertexCount;
                UnityEditor.MeshUtility.SetMeshCompression(_sharedMesh, GetModelImporterMeshCompression(_meshCompression));
                if (_optimizeMesh)
                {
                    _sharedMesh.Optimize();
                }
                if (_generateNormals)
                {
                    Vector3[] normals = new Vector3[vertexCount];
                    for (int i = 0; i < vertexCount; i++)
                    {
                        normals[i] = -Vector3.forward;
                    }
                    _sharedMesh.normals = normals;
                    if (_generateTangents)
                    {
                        Vector4[] tangents = new Vector4[vertexCount];
                        for (int i = 0; i < vertexCount; i++)
                        {
                            tangents[i] = new Vector4(-1f, 0f, 0f, -1f);
                        }
                        _sharedMesh.tangents = tangents;
                    }
                }
            }

            _lastTimeModified = System.DateTime.UtcNow.Ticks;
            UnityEditor.EditorUtility.SetDirty(this);
        }