public void OnEnable() { m_SpriteAsset = target as SpriteAsset; m_FaceInfoProperty = serializedObject.FindProperty("m_FaceInfo"); m_PointSizeProperty = m_FaceInfoProperty.FindPropertyRelative("m_PointSize"); m_ScaleProperty = m_FaceInfoProperty.FindPropertyRelative("m_Scale"); m_LineHeightProperty = m_FaceInfoProperty.FindPropertyRelative("m_LineHeight"); m_AscentLineProperty = m_FaceInfoProperty.FindPropertyRelative("m_AscentLine"); m_BaselineProperty = m_FaceInfoProperty.FindPropertyRelative("m_Baseline"); m_DescentLineProperty = m_FaceInfoProperty.FindPropertyRelative("m_DescentLine"); m_spriteAtlas_prop = serializedObject.FindProperty("m_SpriteAtlasTexture"); m_material_prop = serializedObject.FindProperty("m_Material"); m_SpriteCharacterTableProperty = serializedObject.FindProperty("m_SpriteCharacterTable"); m_SpriteGlyphTableProperty = serializedObject.FindProperty("m_SpriteGlyphTable"); // Fallback TMP Sprite Asset list m_fallbackSpriteAssetList = new ReorderableList(serializedObject, serializedObject.FindProperty("fallbackSpriteAssets"), true, true, true, true); m_fallbackSpriteAssetList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { var element = m_fallbackSpriteAssetList.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2; EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none); }; m_fallbackSpriteAssetList.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, new GUIContent("Fallback Sprite Asset List", "Select the Sprite Assets that will be searched and used as fallback when a given sprite is missing from this sprite asset.")); }; }
void Initialize() { SpriteGraphic[] _spriteGraphic = GetComponentsInChildren <SpriteGraphic>(); for (int i = 0; i < _spriteGraphic.Length; i++) { SpriteAsset _spriteAsset = _spriteGraphic[i].m_spriteAsset; if (!_IndexSpriteGraphic.ContainsKey(_spriteAsset.ID) && !_IndexSpriteInfo.ContainsKey(_spriteAsset.ID)) { SpriteGraphicInfo _spriteGraphicInfo = new SpriteGraphicInfo() { _SpriteGraphic = _spriteGraphic[i], _Mesh = new Mesh(), }; _IndexSpriteGraphic.Add(_spriteAsset.ID, _spriteGraphicInfo); Dictionary <string, SpriteInforGroup> _spriteGroup = new Dictionary <string, SpriteInforGroup>(); foreach (var item in _spriteAsset.listSpriteGroup) { if (!_spriteGroup.ContainsKey(item.tag) && item.listSpriteInfor != null && item.listSpriteInfor.Count > 0) { _spriteGroup.Add(item.tag, item); } } _IndexSpriteInfo.Add(_spriteAsset.ID, _spriteGroup); _TextMeshInfo.Add(_spriteAsset.ID, new Dictionary <InlineText, MeshInfo>()); } } }
public static void AddEffectSpriteObj(string spriteName, GameObject resObj) { if (effectsMap.ContainsKey(spriteName)) { return; } if (resObj == null) { return; } SpriteAsset sa = resObj.GetComponent <SpriteAsset>(); if (sa == null) { return; } Sprite sprite = sa.GetSpriteByName(spriteName); if (sprite == null) { Debug.LogWarning("The sprite asset " + spriteName + " not in Atlas"); } else { effectsMap.Add(spriteName, sprite); } }
static void GenerateSpriteInfo(string unitypath) { Texture2D sourceTex = AssetDatabase.LoadAssetAtPath <Texture2D>(unitypath); if (sourceTex != null) { string Extensionname = Path.GetExtension(unitypath); string filePath = unitypath.Replace(Extensionname, ".asset"); SpriteAsset spriteAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(SpriteAsset)) as SpriteAsset; int tag = 0; if (spriteAsset != null) { tag = spriteAsset.ID; } //replace spriteAsset = ScriptableObject.CreateInstance <SpriteAsset>(); spriteAsset.ID = tag; spriteAsset.AssetName = Path.GetFileNameWithoutExtension(filePath); spriteAsset.texSource = sourceTex; spriteAsset.listSpriteGroup = GetAssetSpriteInfor(sourceTex); AssetDatabase.CreateAsset(spriteAsset, filePath); } }
/// <summary> /// 创建或者更新一个图集的资源信息 /// </summary> /// <param name="targetObj"></param> /// <param name="fullFileName"></param> public static void CreateOrUpdateSpriteAsset(Object targetObj, string fullFileName) { if (null == targetObj || targetObj.GetType() != typeof(Texture2D)) { return; } Texture2D texture2D = targetObj as Texture2D; //获取图集的完整路径 if (string.IsNullOrEmpty(fullFileName)) { fullFileName = AssetDatabase.GetAssetPath(texture2D); } //截取带后缀的文件名 string fileNameWithExtension = Path.GetFileName(fullFileName); //不带后缀名的文件名 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullFileName); //不带文件名的纯路径 string filePath = fullFileName.Replace(fileNameWithExtension, ""); string assetPath = string.Format("{0}{1}.prefab", filePath, fileNameWithoutExtension); SpriteAsset spriteAsset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(SpriteAsset)) as SpriteAsset; if (null == spriteAsset) { GameObject prefab = null; GameObject tempObj = new GameObject(fileNameWithoutExtension); prefab = PrefabUtility.CreatePrefab(assetPath, tempObj); spriteAsset = prefab.AddComponent <SpriteAsset>(); GameObject.DestroyImmediate(tempObj); } spriteAsset.SpriteAssetInfos = GetSpriteAssetInfos(texture2D); EditorUtility.SetDirty(spriteAsset.gameObject); AssetDatabase.SaveAssets(); }
public BattleFish(EntityManager entityManager, string entityName, FishType fishType, bool flipped = false) : base(entityManager, entityName) { _fishType = fishType; _flipped = flipped; _spriteAsset = FishData.FishSpriteAssets[_fishType]; }
public static void AddEffectSprite(string spriteName, string path) { Action <UnityEngine.Object> callback = (asset) => { if (asset != null) { GameObject go = asset as GameObject; SpriteAsset sa = go.GetComponent <SpriteAsset>(); Sprite sprite = sa.GetSpriteByName(spriteName); if (sprite == null) { Debug.LogWarning("The sprite asset " + spriteName + " not in Atlas"); } else { if (!effectsMap.ContainsKey(spriteName)) { effectsMap.Add(spriteName, sprite); } } } else { Debug.LogWarning("Failed to load Font " + spriteName); } }; //AssetUtility.ResourceManager.AsyncLoadResourceAndCleanUp(path, false, false, callback); }
private string GetOutputText() { _imagesTagInfoList.Clear(); IList <UIVertex> verts = cachedTextGenerator.verts; StringBuilder textBuilder = new StringBuilder(); int textIndex = 0; foreach (Match match in _inputRegex.Matches(text)) { int tempId = 0; if (!string.IsNullOrEmpty(match.Groups[1].Value) && !match.Groups[1].Value.Equals("-")) { tempId = int.Parse(match.Groups[1].Value); } string tempTag = match.Groups[2].Value; //更新超链接 if (tempId < 0) { textBuilder.Append(text.Substring(textIndex, match.Index - textIndex)); textBuilder.Append("<color=blue>"); int _startIndex = textBuilder.Length * 4; textBuilder.Append("[" + match.Groups[2].Value + "]"); int _endIndex = textBuilder.Length * 4 - 2; textBuilder.Append("</color>"); var hrefInfo = new HrefInfo { id = Mathf.Abs(tempId), startIndex = _startIndex, // 超链接里的文本起始顶点索引 endIndex = _endIndex, name = match.Groups[2].Value }; _listHrefInfos.Add(hrefInfo); } //更新表情 else { textBuilder.Append(text.Substring(textIndex, match.Index - textIndex)); SpriteAsset spriteAsset = SpriteFaceCache.GetAsset(tempId); SpriteInforGroup tempGroup = SpriteFaceCache.GetAsset(tempId, tempTag); float imgSize = spriteAsset == null ? 24.0f : spriteAsset.size; float imgWidth = spriteAsset == null ? 1.0f : spriteAsset.width; int vertexIndex = textBuilder.Length * 4; textBuilder.Append(@"<quad size=" + imgSize + " width=" + imgWidth + " />"); SpriteTagInfo spInfo = new SpriteTagInfo(); spInfo.vertId = vertexIndex; spInfo.faceId = tempId; spInfo.action = tempTag; spInfo.size = new Vector2(imgSize * imgWidth, imgSize); _imagesTagInfoList.Add(spInfo); } textIndex = match.Index + match.Length; } textBuilder.Append(text.Substring(textIndex, text.Length - textIndex)); return(textBuilder.ToString()); }
void Initialize() { SpriteGraphic[] spriteGraphics = GetComponentsInChildren <SpriteGraphic>(); for (int i = 0; i < spriteGraphics.Length; i++) { SpriteAsset mSpriteAsset = spriteGraphics[i].m_spriteAsset; if (!_indexSpriteGraphic.ContainsKey(mSpriteAsset.Id) && !IndexSpriteInfo.ContainsKey(mSpriteAsset.Id)) { SpriteGraphicInfo spriteGraphicInfo = new SpriteGraphicInfo() { SpriteGraphic = spriteGraphics[i], Mesh = new Mesh(), }; _indexSpriteGraphic.Add(mSpriteAsset.Id, spriteGraphicInfo); Dictionary <string, SpriteInforGroup> spriteGroup = new Dictionary <string, SpriteInforGroup>(); foreach (var item in mSpriteAsset.ListSpriteGroup) { if (!spriteGroup.ContainsKey(item.Tag) && item.ListSpriteInfor != null && item.ListSpriteInfor.Count > 0) { spriteGroup.Add(item.Tag, item); } } IndexSpriteInfo.Add(mSpriteAsset.Id, spriteGroup); _textMeshInfo.Add(mSpriteAsset.Id, new Dictionary <InlineText, MeshInfo>()); } } }
static void main() { 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, ""); SpriteAsset spriteAsset = AssetDatabase.LoadAssetAtPath(filePath + fileNameWithoutExtension + ".asset", typeof(SpriteAsset)) as SpriteAsset; bool isNewAsset = spriteAsset == null ? true : false; if (isNewAsset) { spriteAsset = ScriptableObject.CreateInstance <SpriteAsset>(); spriteAsset.TexSource = sourceTex; spriteAsset.ListSpriteGroup = GetAssetSpriteInfor(sourceTex); AssetDatabase.CreateAsset(spriteAsset, filePath + fileNameWithoutExtension + ".asset"); } }
public void OnEnable() { _spriteAsset = (SpriteAsset)target; if (_spriteAsset) { SetDrawSpriteAsset(_spriteAsset); } }
void DrawSpriteGlyph(Rect position, SerializedProperty property) { // Get a reference to the sprite glyph table SpriteAsset spriteAsset = property.serializedObject.targetObject as SpriteAsset; if (spriteAsset == null) { return; } int glyphIndex = property.FindPropertyRelative("m_GlyphIndex").intValue; // Lookup glyph and draw glyph (if available) int elementIndex = spriteAsset.spriteGlyphTable.FindIndex(item => item.index == glyphIndex); if (elementIndex != -1) { // Get a reference to the Sprite Glyph Table SerializedProperty prop_SpriteGlyphTable = property.serializedObject.FindProperty("m_SpriteGlyphTable"); SerializedProperty prop_SpriteGlyph = prop_SpriteGlyphTable.GetArrayElementAtIndex(elementIndex); SerializedProperty prop_GlyphRect = prop_SpriteGlyph.FindPropertyRelative("m_GlyphRect"); // Get a reference to the sprite texture Texture tex = spriteAsset.spriteSheet; // Return if we don't have a texture assigned to the sprite asset. if (tex == null) { Debug.LogWarning("Please assign a valid Sprite Atlas texture to the [" + spriteAsset.name + "] Sprite Asset.", spriteAsset); return; } Vector2 spriteTexPosition = new Vector2(position.x, position.y); Vector2 spriteSize = new Vector2(48, 48); Vector2 alignmentOffset = new Vector2((58 - spriteSize.x) / 2, (58 - spriteSize.y) / 2); float x = prop_GlyphRect.FindPropertyRelative("m_X").intValue; float y = prop_GlyphRect.FindPropertyRelative("m_Y").intValue; float spriteWidth = prop_GlyphRect.FindPropertyRelative("m_Width").intValue; float spriteHeight = prop_GlyphRect.FindPropertyRelative("m_Height").intValue; if (spriteWidth >= spriteHeight) { spriteSize.y = spriteHeight * spriteSize.x / spriteWidth; spriteTexPosition.y += (spriteSize.x - spriteSize.y) / 2; } else { spriteSize.x = spriteWidth * spriteSize.y / spriteHeight; spriteTexPosition.x += (spriteSize.y - spriteSize.x) / 2; } // Compute the normalized texture coordinates Rect texCoords = new Rect(x / tex.width, y / tex.height, spriteWidth / tex.width, spriteHeight / tex.height); GUI.DrawTextureWithTexCoords(new Rect(spriteTexPosition.x + alignmentOffset.x, spriteTexPosition.y + alignmentOffset.y, spriteSize.x, spriteSize.y), tex, texCoords, true); } }
//添加标签 //private bool addTag; //添加的标签的名称 //private string addTagName; public void OnEnable() { spriteAsset = (SpriteAsset)target; playSpeed = 0.6f; Init(); //EditorApplication.update += RefreshFrameAnimation; }
/// <summary> /// Create and add new default material to sprite asset. /// </summary> /// <param name="spriteAsset"></param> static void AddDefaultMaterial(SpriteAsset spriteAsset) { Shader shader = TextShaderUtilities.ShaderRef_Sprite; Material material = new Material(shader); material.SetTexture(TextShaderUtilities.ID_MainTex, spriteAsset.spriteSheet); spriteAsset.material = material; material.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(material, spriteAsset); }
static void UpdateSpriteAsset(MenuCommand command) { SpriteAsset spriteAsset = (SpriteAsset)command.context; if (spriteAsset == null) { return; } UpdateSpriteAsset(spriteAsset); }
/// <summary> /// 打开资源窗口 /// </summary> /// <param name="spriteAsset"></param> public static void OpenAsset(SpriteAsset spriteAsset) { if (spriteAsset != null && spriteAsset.TexSource != null) { _spriteAsset = spriteAsset; _sourceTex = (Texture2D)_spriteAsset.TexSource; SetDrawSpriteAsset(_spriteAsset); _assetPath = AssetDatabase.GetAssetPath(_spriteAsset); GetWindow <CreateSpriteAsset>("Text Sprite Asset Window"); } }
static void AddDefaultMaterial(MenuCommand command) { SpriteAsset spriteAsset = (SpriteAsset)command.context; // Make sure the sprite asset already contains a default material if (spriteAsset != null && spriteAsset.material == null) { // Add new default material for sprite asset. AddDefaultMaterial(spriteAsset); } }
//绘制信息的类 private static void SetDrawSpriteAsset(SpriteAsset spriteAsset) { //添加 if (_drawSpriteAsset == null) { _drawSpriteAsset = new DrawSpriteAsset(spriteAsset); } else { _drawSpriteAsset.SetSpriteAsset(spriteAsset); } }
static public int get_texSource(IntPtr l) { try { SpriteAsset self = (SpriteAsset)checkSelf(l); pushValue(l, true); pushValue(l, self.texSource); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int constructor(IntPtr l) { try { SpriteAsset o; o = new SpriteAsset(); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int set_listSpriteGroup(IntPtr l) { try { SpriteAsset self = (SpriteAsset)checkSelf(l); System.Collections.Generic.List <SpriteInforGroup> v; checkType(l, 2, out v); self.listSpriteGroup = v; pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int set_texSource(IntPtr l) { try { SpriteAsset self = (SpriteAsset)checkSelf(l); UnityEngine.Texture v; checkType(l, 2, out v); self.texSource = v; pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int set__IsStatic(IntPtr l) { try { SpriteAsset self = (SpriteAsset)checkSelf(l); System.Boolean v; checkType(l, 2, out v); self._IsStatic = v; pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static int SplitTextureToPng(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); SpriteAsset obj = (SpriteAsset)ToLua.CheckObject <SpriteAsset>(L, 1); obj.SplitTextureToPng(); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void LoadAssets(Engine engine) { SpriteAsset image = new SpriteAsset("../../assets/bar.png"); int tileWidth = image.Width / rows; int tileHeight = image.Height / cols; for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { engine.LoadAsset($"bar_{x}_{y}", new SpriteAsset("../../assets/bar.png", x * tileWidth, y * tileHeight, tileWidth, tileHeight)); } } }
static int GetSpriteByName(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); SpriteAsset obj = (SpriteAsset)ToLua.CheckObject <SpriteAsset>(L, 1); string arg0 = ToLua.CheckString(L, 2); UnityEngine.Sprite o = obj.GetSpriteByName(arg0); ToLua.PushSealed(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private static void LoadAssetCom(Object target, string path) { SpriteAsset spAsset = target as SpriteAsset; if (null != spAsset) { _spAssetDic[spAsset.ID] = spAsset; } _loadIndex++; if (_loadIndex >= _pathList.Count) { GameStartEvent.getInstance().dispatchEvent(GameLoadStepEvent.LOAD_FACE_ASSET); return; } LoadAsset(); }
static int set_SpriteAssetInfos(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); SpriteAsset obj = (SpriteAsset)o; System.Collections.Generic.List <SpriteAssetInfo> arg0 = (System.Collections.Generic.List <SpriteAssetInfo>)ToLua.CheckObject(L, 2, typeof(System.Collections.Generic.List <SpriteAssetInfo>)); obj.SpriteAssetInfos = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index SpriteAssetInfos on a nil value")); } }
static int get_SpriteAssetInfos(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); SpriteAsset obj = (SpriteAsset)o; System.Collections.Generic.List <SpriteAssetInfo> ret = obj.SpriteAssetInfos; ToLua.PushSealed(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index SpriteAssetInfos on a nil value")); } }
public static void LoadAnimation(Engine engine, string name, string fileName, int xLen, int yLen) { var spriteAsset = new SpriteAsset(fileName); var blockSizeOnWall = new Vector2(spriteAsset.Width / (float)xLen, spriteAsset.Height / (float)yLen); for (var posX = 0; posX < xLen; posX++) { for (var posY = 0; posY < yLen; posY++) { var animName = $"{name}_{posY}_{posX}"; Debug.WriteLine("Loaded animations: " + animName); engine.LoadAsset(animName, new SpriteAsset(fileName, (int)(posX * blockSizeOnWall.X), (int)(posY * blockSizeOnWall.Y), (int)blockSizeOnWall.X, (int)blockSizeOnWall.Y)); } } }
// Update exsisting SpriteInfo private static List<SpriteInfo> UpdateSpriteInfo(SpriteAsset spriteAsset) { //Debug.Log("Updating Sprite Asset."); string filePath = UnityEditor.AssetDatabase.GetAssetPath(spriteAsset.spriteSheet); // Get all the Sprites sorted Left to Right / Top to Bottom Sprite[] sprites = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(filePath).Select(x => x as Sprite).Where(x => x != null).OrderByDescending(x => x.rect.y).ThenBy(x => x.rect.x).ToArray(); for (int i = 0; i < sprites.Length; i++) { Sprite sprite = sprites[i]; // Check if sprite already exists in the SpriteInfoList int index = spriteAsset.spriteInfoList.FindIndex(item => item.name == sprite.name); //Debug.Log("") // Use existing SpriteInfo is it already exists SpriteInfo spriteInfo = index == -1 ? new SpriteInfo() : spriteAsset.spriteInfoList[index]; Rect spriteRect = sprite.rect; spriteInfo.name = sprite.name; spriteInfo.x = spriteRect.x; spriteInfo.y = spriteRect.y; spriteInfo.width = spriteRect.width; spriteInfo.height = spriteRect.height; // Get Sprite Pivot Vector2 pivot = new Vector2(0 - (sprite.bounds.min.x) / (sprite.bounds.extents.x * 2), 0 - (sprite.bounds.min.y) / (sprite.bounds.extents.y * 2)); // The position of the pivot influences the Offset position. spriteInfo.pivot = new Vector2(0 - pivot.x * spriteRect.width, spriteRect.height - pivot.y * spriteRect.height); if (index == -1) { // Find the next available index for this Sprite int[] ids = spriteAsset.spriteInfoList.Select(item => item.id).ToArray(); int id = 0; for (int j = 0; j < ids.Length; j++ ) { if (ids[0] != 0) break; if (j > 0 && (ids[j] - ids[j - 1]) > 1) { id = ids[j - 1] + 1; break; } id = j + 1; } //spriteInfo.fileID = fileID; spriteInfo.id = id; spriteInfo.xAdvance = spriteRect.width; spriteInfo.scale = 1.0f; spriteInfo.xOffset = 0; spriteInfo.yOffset = 0; spriteAsset.spriteInfoList.Add(spriteInfo); } else { spriteAsset.spriteInfoList[index] = spriteInfo; } } return spriteAsset.spriteInfoList; }