static void CreateEmojiAsset(string path, Texture2D[] textures) { EmojiAsset emojiAsset = AssetDatabase.LoadAssetAtPath(path, typeof(EmojiAsset)) as EmojiAsset; if (null == emojiAsset) { emojiAsset = ScriptableObject.CreateInstance <EmojiAsset>(); AssetDatabase.CreateAsset(emojiAsset, path); } string ext = Path.GetExtension(path); string atlasPath = path.Replace(ext, ".png"); var atlas = CreateAtlas(textures, atlasPath); List <SpriteInfo> spriteInfoList = new List <SpriteInfo>(); var totalFrame = 0; for (int i = 0; i < atlas.frames.Count; i++) { var frames = atlas.frames[i]; var name = frames[0].name; var index = totalFrame; var frameCount = frames.Count; spriteInfoList.Add(new SpriteInfo(name, index, frameCount)); totalFrame += frameCount; } emojiAsset.spriteInfoList = spriteInfoList; emojiAsset.spriteSheet = atlas.atlas; emojiAsset.emojiSize = atlas.emojiSize; emojiAsset.column = atlas.column; AddDefaultMaterial(emojiAsset); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
static void CreateEmojiAsset() { Object target = Selection.activeObject; if (target == null || target.GetType() != typeof(Texture2D)) { return; } Texture2D sourceTex = target as Texture2D; //整体路径 string filePathWithName = AssetDatabase.GetAssetPath(sourceTex); //带后缀的文件名 string fileNameWithExtension = Path.GetFileName(filePathWithName); //不带后缀的文件名 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePathWithName); //不带文件名的路径 string filePath = filePathWithName.Replace(fileNameWithExtension, ""); EmojiAsset spriteAsset = AssetDatabase.LoadAssetAtPath(filePath + fileNameWithoutExtension + ".asset", typeof(EmojiAsset)) as EmojiAsset; bool isNewAsset = spriteAsset == null ? true : false; if (isNewAsset) { spriteAsset = ScriptableObject.CreateInstance <EmojiAsset>(); spriteAsset.texSource = sourceTex; spriteAsset.listSpriteGroup = GetAssetSpriteInfor(sourceTex); AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset"); } }
public void OnEnable() { spriteAsset = (EmojiAsset)target; playSpeed = 6; Init(); // EditorApplication.update += RefreshFrameAnimation; }
/// <summary> /// Create and add new default material to emoji asset. /// </summary> /// <param name="asset"></param> static void AddDefaultMaterial(EmojiAsset asset) { if (null == asset) { return; } var material = asset.material; if (null == material) { material = new Material(Shader.Find("UI/EmojiFont")); asset.material = material; AssetDatabase.AddObjectToAsset(material, asset); } material.SetTexture("_EmojiTex", asset.spriteSheet); material.SetFloat("_EmojiSize", asset.emojiSize); material.SetFloat("_Column", asset.column); material.hideFlags = HideFlags.HideInHierarchy; }