Example #1
0
        public override Atlas Parse()
        {
            if (!ValidXML())
            {
                return(null);
            }

            Vector2          sheetSize = Vector2.zero;
            List <AtlasData> data      = new List <AtlasData>();
            string           name      = null;

            if (xml.DocumentElement.Name == "plist")
            {
                XmlNodeList nodeList = xml.DocumentElement.SelectNodes("dict/key");
                for (int i = 0; i < nodeList.Count; i++)
                {
                    XmlNode frames = nodeList[i];
                    if (frames != null && frames.InnerText == "frames")
                    {
                        XmlNodeList subTextureNames = xml.DocumentElement.SelectNodes("dict/dict/key");
                        XmlNodeList subTextures     = xml.DocumentElement.SelectNodes("dict/dict/dict");
                        try {
                            for (int si = 0; si < subTextures.Count; si++)
                            {
                                subTexture = subTextures[si];
                                AtlasData ad = new AtlasData();

                                bool    rotated    = GetBool("rotated");
                                Rect    frame      = GetRect("frame");
                                Rect    colorRect  = GetRect("sourceColorRect");
                                Vector2 sourceSize = GetVector2("sourceSize");
                                Vector2 offset     = GetVector2("offset");

                                try {
                                    ad.name = subTextureNames[si].InnerText.Split('.')[0];
                                } catch (System.Exception) {
                                    ad.name = subTextureNames[si].InnerText;
                                }
                                ad.position = new Vector2(frame.xMin, frame.yMin);
                                if (rotated)
                                {
                                    ad.rotated = true;
                                }

                                ad.size      = new Vector2(colorRect.width, colorRect.height);
                                ad.frameSize = sourceSize;
                                ad.offset    = offset;

                                data.Add(ad);
                            }
                        } catch (System.Exception ERR) {
                            Debug.LogError("Orthello : Cocos2D Atlas Import error!");
                            Debug.LogError(ERR.Message);
                        }
                    }
                    else if (frames != null && frames.InnerText == "metadata")
                    {
                        XmlNode sizeNode = frames.NextSibling.SelectSingleNode("key[text()=\"size\"]");
                        if (sizeNode != null)
                        {
                            sheetSize = StringToVector2(sizeNode.NextSibling.InnerText);
                        }

                        XmlNode nameNode = frames.NextSibling.SelectSingleNode("key[text()=\"realTextureFileName\"]");
                        if (nameNode == null)
                        {
                            nameNode = frames.NextSibling.SelectSingleNode("key[text()=\"textureFileName\"]");
                        }
                        if (nameNode != null)
                        {
                            string[] sa = nameNode.NextSibling.InnerText.Split('.');
                            if (sa.Length > 0)
                            {
                                name = sa[0];
                            }
                        }
                    }
                }
            }

            Atlas atlas = new Atlas();

            atlas.atlasData = data.ToArray();
            atlas.sheetSize = sheetSize;
            atlas.name      = name;

            return(atlas);
        }
Example #2
0
        public static void Make(TextAsset textAsset)
        {
            // input
            // Assets/Tang/Input/MultiSet/Data/mount_0.xml
            string textAssetPath    = AssetDatabase.GetAssetPath(textAsset);
            string textAssetDirPath = textAssetPath.Substring(0, textAssetPath.LastIndexOf('/'));
            string texturePath      = textAssetPath.Replace("Data", "Textures").Replace(".xml", ".png");
            // output
            string matAlphaPath  = textAssetPath.Replace("Input", "Output").Replace("Data", "Materials").Replace(".xml", "-alpha.mat");
            string prefabDirPath = textAssetDirPath.Replace("Input", "Output").Replace("Data", "Prefabs");

            // parameters
            string[] splits  = textAssetPath.Split(new Char[] { '/' });
            string   resName = splits[splits.Length - 1].Split(new Char[] { '.' })[0];

            // parse data
            AtlasParser parser = new AtlasCocos2DParser(textAsset.text);
            Atlas       atls   = parser.Parse();

            MidFrame[] midFrames = atls.GetMidFrames();

            // create material
            Material matAlpha = new Material(Shader.Find("Mobile/Particles/Alpha Blended"));

            matAlpha.name        = resName + "_alpha";
            matAlpha.mainTexture = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture)) as Texture;
            AssetDatabase.CreateAsset(matAlpha, matAlphaPath);

            string spriteType = splits[splits.Length - 3];

            if ("MultiSet".Equals(spriteType))
            {
                Dictionary <string, List <Frame> > frameTable = new Dictionary <string, List <Frame> >();
                foreach (MidFrame frame in midFrames)
                {
                    string[] frameNamesplits = frame.name.Split(new Char[] { '_' });
                    string   key             = frameNamesplits[0];
                    //string index = frameNamesplits[1];

                    List <Frame> frameList = null;
                    if (!frameTable.ContainsKey(key))
                    {
                        frameList = new List <Frame>();
                        frameTable.Add(key, frameList);
                    }
                    else
                    {
                        frameList = frameTable[key];
                    }

                    frameList.Add(frame);
                }

                List <FrameSet> frameSets = new List <FrameSet>();
                foreach (KeyValuePair <string, List <Frame> > kvp in frameTable)
                {
                    FrameSet frameSet = new FrameSet(kvp.Key, kvp.Value.ToArray());
                    frameSets.Add(frameSet);
                }


                // create game object
                GameObject go = new GameObject();
                go.name = resName;

                MultiSetSprite sprite = go.AddComponent <MultiSetSprite>();
                sprite.frameSets = frameSets.ToArray();

                // mesh reader
                MeshRenderer mr = go.AddComponent <MeshRenderer>();
                mr.material       = matAlpha;
                mr.castShadows    = false;
                mr.receiveShadows = false;

                // create prefab
                // output prefab path
                string             prefabPath = prefabDirPath + "/" + resName + ".prefab";
                UnityEngine.Object prefab     = PrefabUtility.CreateEmptyPrefab(prefabPath);
                PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);

                // destory game object
                GameObject.DestroyImmediate(go);
            }
            else if ("MultiFrame".Equals(spriteType))
            {
                // create game object
                GameObject go = new GameObject();
                go.name = resName;

                MultiFrameSprite sprite = go.AddComponent <MultiFrameSprite>();
                sprite.frames = midFrames;

                // mesh reader
                MeshRenderer mr = go.AddComponent <MeshRenderer>();
                mr.material       = matAlpha;
                mr.castShadows    = false;
                mr.receiveShadows = false;

                // create prefab
                // output prefab path
                string             prefabPath = prefabDirPath + "/" + resName + ".prefab";
                UnityEngine.Object prefab     = PrefabUtility.CreateEmptyPrefab(prefabPath);
                PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);

                // destory game object
                GameObject.DestroyImmediate(go);
            }
            else if ("SingleFrame".Equals(spriteType))
            {
                foreach (MidFrame frame in midFrames)
                {
                    GameObject go = new GameObject();
                    go.name = resName + frame.name.Replace('/', '_');

                    SingleFrameSprite sprite = go.AddComponent <SingleFrameSprite>();
                    sprite.fr = frame;

                    // mesh reader
                    MeshRenderer mr = go.AddComponent <MeshRenderer>();
                    mr.material       = matAlpha;
                    mr.castShadows    = false;
                    mr.receiveShadows = false;

                    // create prefab
                    // output prefab path
                    string             prefabPath = prefabDirPath + "/" + go.name + ".prefab";
                    UnityEngine.Object prefab     = PrefabUtility.CreateEmptyPrefab(prefabPath);
                    PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);

                    // destory game object
                    GameObject.DestroyImmediate(go);
                }
            }
        }