Beispiel #1
0
    Load(string srcPath)
    {
        _database = SsAssetPostProcessor.GetDatabase();
        string path = Path.GetDirectoryName(srcPath);

        // new path name of ss anime asset
        string newPath = path + "/assets/" + Path.GetFileNameWithoutExtension(srcPath) + "_ssa.asset";

        // identify the encoding which is Shift-JIS or UTF-8
        Encoding encode = SsEncoding.SJIS;
        {
            StreamReader sr            = File.OpenText(srcPath);
            string       xmlHeaderText = sr.ReadLine();
            // XmlDocument requires one node at least and also needs to get "encoding" attribute.
            string dummy = System.String.Copy(xmlHeaderText);
            dummy          = dummy.Replace("<?xml", "<Dummy");
            dummy          = dummy.Replace("?>", "/>");
            xmlHeaderText += dummy;
            sr.Close();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlHeaderText);
            XmlNode node   = doc.FirstChild.NextSibling;
            string  encStr = node.Attributes["encoding"].Value;
            if (encStr == "shift_jis")
            {
                encode = SsEncoding.SJIS;
            }
            else if (encStr == "utf-8")
            {
                encode = SsEncoding.UTF8;
            }
        }

        // to read SJIS encoded text correctly.
        string textAll = File.ReadAllText(srcPath, encode);

        // create directory if it doesn't exist.
        bool newAsset = false;

        if (!Directory.Exists(path + "/assets"))
        {
            AssetDatabase.CreateFolder(path, "assets");
        }
        else
        {
            // try to open existing asset
            _anmRes = AssetDatabase.LoadAssetAtPath(newPath, typeof(SsAnimation)) as SsAnimation;
        }

        if (_anmRes == null)
        {
            // create new asset
            _anmRes  = ScriptableObject.CreateInstance <SsAnimation>();
            newAsset = true;
        }
        else
        {
            // update existing asset
            ClearPreviousMaterials();
            _anmRes.ImageList = null;
            _anmRes.PartList  = null;
            System.GC.Collect();
        }

        // update imported time
        _anmRes.UpdateImportedTime();

        // save original resource path
        _anmRes.OriginalPath = srcPath;

        // save scale factor at this import.
        if (!_anmRes.UseScaleFactor)
        {
            _anmRes.ScaleFactor = _database.ScaleFactor;
        }

#if _BUILD_UNIFIED_SHADERS
        if (_anmRes._UseUnifiedShader == UseUnifiedShaderEnum.Default)
        {
            // use global setting
            _anmRes.UseUnifiedShader = _database.UseUnifiedShader;
        }
        else
        {
            // use own setting
            _anmRes.UseUnifiedShader = (_anmRes._UseUnifiedShader == UseUnifiedShaderEnum.Yes);
        }
#endif

        if (!LoadXml(path, textAll))
        {
            Debug.LogError("Failed to import animation data: " + srcPath);
            return(false);
        }

        if (newAsset)
        {
            AssetDatabase.CreateAsset(_anmRes, newPath);
        }
        else
        {
            // modification done internally is not applied actual file, so must make it dirty before.
            EditorUtility.SetDirty(_anmRes);
            AssetDatabase.SaveAssets();                 //same as EditorApplication.SaveAssets();
        }

#if false
        // AddObjectToAsset() a_mat.mat -> hoge.ssax.asset
        // ↓
        // hoge.ssax.mat
        //   hoge.ssax.asset
        // umm...
        foreach (var e in _anmRes.ImageList)
        {
            if (e.material != null)
            {
                e.material.name = e.texture.name + "_Mat";
                AssetDatabase.AddObjectToAsset(e.material, _anmRes);

                // Reimport the asset after adding an object.
                // Otherwise the change only shows up when saving the project
                AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(e.material));
            }
        }
#endif
        return(true);
    }