Beispiel #1
0
    private static void CreateCharacterPrefabs(string assetPath)
    {
        //EditorUtility.SetDirty(texture);

        //AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        //AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

        string[] split = assetPath.Split('/');
        string   name  = split[split.Length - 1];

        name = name.Replace(".png", "");

        Object[] SpriteAssetArr = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(assetPath);
        if (SpriteAssetArr.Length <= 0)
        {
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        }
        else
        {
            //GameObject gocopy = new GameObject(name);
            //GameObject gocopy = EditorUtility.CreateGameObjectWithHideFlags(name, HideFlags.HideInHierarchy);
            //AutoTileSprite autoTileResource = gocopy.AddComponent<AutoTileSprite>();



            //             CharacterSpritePackerAsset characterSprite = ScriptableObject.CreateInstance<CharacterSpritePackerAsset>();
            //             characterSprite.AssetName = name;
            //             characterSprite.ListPack = new CharacterSpritePackerAsset.CharacterSpritePacker[11 * 18];

            string prefabspath = "Assets/Resources/Map/Prefabs/Character";
            if (Directory.Exists(prefabspath) == false)
            {
                MakeDirectory(prefabspath);
            }


            ScriptCsv scriptCsv = new ScriptCsv(prefabspath + "/" + name + ".csv");
            Texture2D texture2D = (Texture2D)SpriteAssetArr[0];
            Sprite    sprite    = (Sprite)SpriteAssetArr[1];
            int       offsetX   = (int)(texture2D.width / sprite.rect.width);

            int index = 0;
            for (int i = 0; i < 11; i++)
            {
                for (int j = 0; j < 18; j++)
                {
                    int offset = i * 36 + 1;
                    SpritePackerAsset spritePackerAsset = ScriptableObject.CreateInstance <SpritePackerAsset>();
                    spritePackerAsset.AllSprites    = new Sprite[2];
                    spritePackerAsset.AllSprites[0] = (Sprite)SpriteAssetArr[j + offset];
                    spritePackerAsset.AllSprites[1] = (Sprite)SpriteAssetArr[j + offset + 18];
                    spritePackerAsset.AssetName     = spritePackerAsset.AllSprites[0].name;

                    string filecheck = prefabspath + "/" + spritePackerAsset.AssetName + ".asset";
                    if (File.Exists(filecheck))
                    {
                        File.Delete(filecheck);
                    }

                    AssetDatabase.CreateAsset(spritePackerAsset, filecheck);

                    SpriteSheetCsv scSheetCsv = new SpriteSheetCsv();
                    scSheetCsv.assetName  = name;
                    scSheetCsv.id         = index;
                    scSheetCsv.spriteName = spritePackerAsset.AllSprites[0].name;
                    scSheetCsv.numIndex   = j + offset - 1;
                    scSheetCsv.numX       = (scSheetCsv.numIndex) % offsetX;
                    scSheetCsv.numY       = (scSheetCsv.numIndex) / offsetX;
                    scriptCsv.LstValue.Add(scSheetCsv);



                    index++;
                }
            }
            scriptCsv.Save();
            SpritePackerAsset spriteshadowPackerAsset = ScriptableObject.CreateInstance <SpritePackerAsset>();
            spriteshadowPackerAsset.AllSprites = new Sprite[6];
            index = 0;
            for (int i = 11 * 36 + 1; i < SpriteAssetArr.Length; i++)
            {
                spriteshadowPackerAsset.AllSprites[index++] = (Sprite)SpriteAssetArr[i];
            }
            spriteshadowPackerAsset.AssetName = name;

            string fileshadowcheck = prefabspath + "/" + name + "_shadow.asset";
            if (File.Exists(fileshadowcheck))
            {
                File.Delete(fileshadowcheck);
            }

            AssetDatabase.CreateAsset(spriteshadowPackerAsset, fileshadowcheck);

            //string basePath = Path.GetDirectoryName(assetPath.Substring(0, assetPath.LastIndexOf(".png")));

            //Object tempPrefab = PrefabUtility.CreateEmptyPrefab(prefabspath + "/" + name + ".prefab");
            //PrefabUtility.ReplacePrefab(gocopy, tempPrefab, ReplacePrefabOptions.ConnectToPrefab);
            //GameObject.DestroyImmediate(gocopy, true);
        }
    }
Beispiel #2
0
    private static void CreateAutoTile16Prefabs(string assetPath)
    {
        string[] split = assetPath.Split('/');
        string   name  = split[split.Length - 1];

        name = name.Replace(".png", "");

        string[] split2  = name.Split('_');
        string   subname = split2[split2.Length - 1];

        //EditorUtility.SetDirty(texture);

        //AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        //AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

        Object[] SpriteAssetArr = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(assetPath);
        if (SpriteAssetArr.Length <= 0)
        {
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        }
        else
        {
            string prefabspath = "Assets/Resources/Map/Prefabs/AutoTile";
            if (Directory.Exists(prefabspath) == false)
            {
                MakeDirectory(prefabspath);
            }


            ScriptCsv scriptCsv = new ScriptCsv(prefabspath + "/" + name + ".csv");
            Texture2D texture2D = (Texture2D)SpriteAssetArr[0];
            int       offset    = texture2D.width / 24;

            int count = (SpriteAssetArr.Length - 2) / offset;
            //autoTileResource.ListPack = new TileSpritePackerAsset.TileSpritePacker[count];
            for (int i = 0; i < count; i++)
            {
                int spriteoffset = i * offset + 1;
                SpritePackerAsset spritePackerAsset = ScriptableObject.CreateInstance <SpritePackerAsset>();
                //TileSpritePackerAsset.TileSpritePacker spritePackerAsset = new TileSpritePackerAsset.TileSpritePacker();
                spritePackerAsset.AssetName  = subname + i.ToString();
                spritePackerAsset.AllSprites = new Sprite[offset];
                for (int j = 0; j < offset; j++)
                {
                    spritePackerAsset.AllSprites[j] = (Sprite)SpriteAssetArr[j + spriteoffset];
                }
                //autoTileResource.ListPack[i] = spritePackerAsset;
                string filecheck = prefabspath + "/" + spritePackerAsset.AssetName + ".asset";
                if (File.Exists(filecheck))
                {
                    File.Delete(filecheck);
                }
                AssetDatabase.CreateAsset(spritePackerAsset, filecheck);

                SpriteSheetCsv scSheetCsv = new SpriteSheetCsv();
                scSheetCsv.assetName  = name;
                scSheetCsv.id         = i;
                scSheetCsv.spriteName = spritePackerAsset.AssetName;
                scSheetCsv.numIndex   = i;
                scSheetCsv.numX       = (scSheetCsv.numIndex) % spriteoffset;
                scSheetCsv.numY       = (scSheetCsv.numIndex) / spriteoffset;
                scriptCsv.LstValue.Add(scSheetCsv);
            }

            scriptCsv.Save();
            //autoTileResource.AssetName = name;
        }
    }
Beispiel #3
0
    private static void CreateSpritePacker(string assetPath, string outpath)
    {
        string[] split = assetPath.Split('/');
        string   name  = split[split.Length - 1];

        name = name.Replace(".png", "");

        //EditorUtility.SetDirty(texture);

        //AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        //AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

        Object[] SpriteAssetArr = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(assetPath);
        if (SpriteAssetArr.Length <= 0)
        {
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        }
        else
        {
            //GameObject gocopy = new GameObject(name);


            //GameObject gocopy = EditorUtility.CreateGameObjectWithHideFlags(name, HideFlags.HideInHierarchy);
            //AutoTileSprite autoTileResource = gocopy.AddComponent<AutoTileSprite>();

            string prefabspath = "Assets/Resources/Map/Prefabs/" + outpath;
            if (Directory.Exists(prefabspath) == false)
            {
                MakeDirectory(prefabspath);
            }
            string filecheck = prefabspath + "/" + name + ".asset";
            if (File.Exists(filecheck))
            {
                File.Delete(filecheck);
            }
            ScriptCsv scriptCsv = new ScriptCsv(prefabspath + "/" + name + ".csv");
            Texture2D texture2D = (Texture2D)SpriteAssetArr[0];
            Sprite    sprite    = (Sprite)SpriteAssetArr[1];

            int offsetX = (int)(texture2D.width / sprite.rect.width);
            int offsetY = (int)(texture2D.height / sprite.rect.height);

            SpritePackerAsset objectPackerSprite = ScriptableObject.CreateInstance <SpritePackerAsset>();
            objectPackerSprite.AllSprites = new Sprite[SpriteAssetArr.Length - 1];
            for (int i = 1; i < SpriteAssetArr.Length; i++)
            {
                objectPackerSprite.AllSprites[i - 1] = (Sprite)SpriteAssetArr[i];
                SpriteSheetCsv scSheetCsv = new SpriteSheetCsv();
                scSheetCsv.assetName  = name;
                scSheetCsv.id         = i - 1;
                scSheetCsv.spriteName = objectPackerSprite.AllSprites[i - 1].name;
                scSheetCsv.numIndex   = scSheetCsv.id;
                scSheetCsv.numX       = scSheetCsv.id % offsetX;
                scSheetCsv.numY       = scSheetCsv.id / offsetX;
                scriptCsv.LstValue.Add(scSheetCsv);
            }
            objectPackerSprite.AssetName = name;
            scriptCsv.Save();

            AssetDatabase.CreateAsset(objectPackerSprite, filecheck);
        }
    }