void PushSliceTitle(int nSpriteID, int nWidth, int nHeight, float fx, float fy, float fBloodPos) { UISpriteInfo sp = CAtlasMng.instance.GetSafeSpriteByID(nSpriteID); if (sp == null) { return; } int nAtlasID = sp.m_nAtlasID; int nStart = m_aSprite.size; for (int i = 0; i < 9; ++i) { HUDVertex node = HUDVertex.QueryVertex(); node.WorldPos = m_vPos; node.ScreenPos = m_vScreenPos; node.SpriteID = nSpriteID; node.AtlasID = nAtlasID; node.Scale = m_fScale; node.Offset.Set(fx, fy); node.Move.Set(0f, 0f); node.width = (short)nWidth; node.height = (short)nHeight; m_aSprite.Add(node); } SlicedFill(nSpriteID, nWidth, nHeight, nStart, fBloodPos); }
static bool IsChangeSprite(UISpriteInfo a, UISpriteInfo b) { if (a == null || b == null) { return(a == b); } if (!IsSameRect(a.outer, b.outer)) { return(true); } if (!IsSameRect(a.inner, b.inner)) { return(true); } int nLeftA = Mathf.RoundToInt(a.paddingLeft * a.outer.width); int nTopA = Mathf.RoundToInt(a.paddingTop * a.outer.height); int nRightA = Mathf.RoundToInt(a.paddingRight * a.outer.width); int nBottomA = Mathf.RoundToInt(a.paddingBottom * a.outer.height); int nLeftB = Mathf.RoundToInt(b.paddingLeft * b.outer.width); int nTopB = Mathf.RoundToInt(b.paddingTop * b.outer.height); int nRightB = Mathf.RoundToInt(b.paddingRight * b.outer.width); int nBottomB = Mathf.RoundToInt(b.paddingBottom * b.outer.height); if (nLeftA != nLeftB || nTopA != nTopB || nRightA != nRightB || nBottomA != nBottomB) { return(true); } return(false); }
/// <summary> /// Draw the sprite preview. /// </summary> public override void OnPreviewGUI(Rect rect, GUIStyle background) { // 绘制预览面板 if (mSprite == null || !mSprite.isValid) { return; } if (m_sprite == null) { return; } UISpriteInfo sp = AtlasMng_Editor.instance.GetSprite(mSprite.name); if (sp == null) { return; } UITexAtlas atlas = AtlasMng_Editor.instance.GetAltasBySpriteName(mSprite.name); if (atlas == null || atlas.m_material == null) { return; } Texture2D tex = atlas.m_material.mainTexture as Texture2D; if (tex == null) { return; } Rect outer = new Rect(sp.outer); Rect inner = new Rect(sp.inner); Rect uv = outer; if (atlas.coordinates == UITexAtlas.Coordinates.Pixels) { uv = HUDMath.ConvertToTexCoords(outer, tex.width, tex.height); } else { outer = HUDMath.ConvertToPixels(outer, tex.width, tex.height, true); inner = HUDMath.ConvertToPixels(inner, tex.width, tex.height, true); } HUDEditorTools.DrawSprite(tex, rect, outer, inner, uv, new Color(1.0f, 1.0f, 1.0f, 1.0f)); }
public void InitSprite(int nWidth = 0, int nHeight = 0) { ch = '\0'; AtlasID = -1; UISpriteInfo sp = CAtlasMng.instance.GetSafeSpriteByID(SpriteID); if (sp == null) { return; } AtlasID = sp.m_nAtlasID; width = nWidth <= 0 ? (short)(sp.outer.width + 0.5f) : (short)nWidth; height = nHeight <= 0 ? (short)(sp.outer.height + 0.5f) : (short)nHeight; Scale = 1.0f; Rect mOuterUV = sp.outer; UITexAtlas texAtlas = CAtlasMng.instance.GetAtlasByID(sp.m_nAtlasID); if (texAtlas != null && texAtlas.coordinates == UITexAtlas.Coordinates.Pixels) { mOuterUV = HUDVertex.ConvertToTexCoords(mOuterUV, texAtlas.texWidth, texAtlas.texHeight); } float fL = 0.0f; float fT = 0.0f; float fR = width; float fB = height; vecRU.Set(fR, fT); // 右上角 vecRD.Set(fR, fB); // 右下角 vecLD.Set(fL, fB); // 左下角 vecLU.Set(fL, fT); // 左上角 float uvR = mOuterUV.xMax; float uvL = mOuterUV.xMin; float uvB = mOuterUV.yMin; float uvT = mOuterUV.yMax; uvRU.Set(uvR, uvB); uvRD.Set(uvR, uvT); uvLD.Set(uvL, uvT); uvLU.Set(uvL, uvB); clrLD = clrLU = clrRD = clrRU = Color.white; }
public void PushIcon(HUDTilteType titleType, int nSpriteID) { HudTitleAttribute titleAttrib = HudSetting.Instance.TitleSets[(int)titleType].GetTitle(0); UISpriteInfo sp = CAtlasMng.instance.GetSafeSpriteByID(nSpriteID); int nWidth = 0; int nHeight = 0; int nStart = m_aSprite.size; if (sp != null) { nWidth = (int)sp.outer.width; nHeight = (int)sp.outer.height; PushSprite(nSpriteID, nWidth, nHeight, 0.0f, titleAttrib.SpriteOffsetY); } if (m_TitleLine[m_nTitleNumb] == null) { m_TitleLine[m_nTitleNumb] = new HUDTilteLine(); } m_TitleLine[m_nTitleNumb].m_nType = titleType; m_TitleLine[m_nTitleNumb].m_fWidth = nWidth; m_TitleLine[m_nTitleNumb].m_nHeight = nHeight; m_TitleLine[m_nTitleNumb].m_nStart = nStart; m_TitleLine[m_nTitleNumb].m_nEnd = m_aSprite.size; m_TitleLine[m_nTitleNumb].m_szText = string.Empty; m_TitleLine[m_nTitleNumb].m_nColorIndex = 0; m_TitleLine[m_nTitleNumb].m_nLine = m_nLines; m_TitleLine[m_nTitleNumb].m_nSpriteID = nSpriteID; if (titleAttrib.AlignType == HUDAlignType.align_center) { m_fCurLineWidth = nWidth; } nHeight += titleAttrib.SpriteReduceHeight; if (nHeight < titleAttrib.LockMaxHeight && titleAttrib.LockMaxHeight > 0) { nHeight = titleAttrib.LockMaxHeight; } if (m_fCurLineHeight < nHeight) { m_fCurLineHeight = nHeight; } ++m_nTitleNumb; }
/// <summary> /// Draw the atlas and sprite selection fields. /// </summary> protected virtual bool DrawProperties() { mSprite = target as MyUISpritePreView; // 属性绘制 string spriteName = mSprite != null ? mSprite.name : ""; if (m_sprite == null || m_sprite.name != spriteName) { UISpriteInfo sprite = AtlasMng_Editor.instance.GetSprite(spriteName); if (m_bDirty) { if (m_sprite != null) { UISpriteInfo oldSprite = AtlasMng_Editor.instance.GetSprite(m_sprite.name); if (oldSprite != null) { oldSprite.Copy(m_sprite); // 自动取消修改 MarkSpriteAsDirty(); } } m_bDirty = false; } if (sprite != null) { m_sprite = sprite.Clone(); } } // Sprite selection drop-down list GUILayout.BeginHorizontal(); { if (string.IsNullOrEmpty(spriteName)) { spriteName = ""; } GUILayout.Label(spriteName, GUILayout.Width(180f)); } GUILayout.EndHorizontal(); return(true); }
void OnDisable() { if (m_bDirty) { UISpriteInfo sprite = AtlasMng_Editor.instance.GetSprite(m_sprite.name); if (EditorUtility.DisplayDialog("提示", "你确定要保存当前的修改么?", "OK", "cancel")) { // 修改吧 //sprite.Copy(m_sprite); AtlasMng_Editor.instance.SaveAltasCfg(); } else { if (sprite != null) { sprite.Copy(m_sprite); // 撤消修改 } } MarkSpriteAsDirty(); m_bDirty = false; } }
void UpdateMeridianNumber(int nMeridianNumb) { // 必须是在最后 if (0 == m_nMeridianIndex || m_nMeridianIndex != m_nTitleNumb - 1) { return; } // 先释放旧的吧 HUDTilteLine title = m_TitleLine[m_nMeridianIndex]; if (title.m_nType != HUDTilteType.HeadIcon) { return; } if (nMeridianNumb < 0) { nMeridianNumb = 0; } m_nMeridianNumb = nMeridianNumb; HudTitleAttribute titleAttrib = HudSetting.Instance.TitleSets[(int)HUDTilteType.HeadIcon].GetTitle(0); int nCharGap = titleAttrib.CharGap; int nPow = 1; while (nPow * 10 <= nMeridianNumb) { nPow *= 10; } int nWidth = 0; int nHeight = 0; int nMaxWidth = 0; int nMaxHeight = 0; int[] PicID = HudSetting.Instance.MeridianPic; int nSpriteID = 0; int nValue = nMeridianNumb; int nStart = title.m_nStart; int nEnd = title.m_nEnd; int nX = 0; while (nPow > 0) { int nIndex = nValue / nPow; nValue %= nPow; nPow /= 10; nSpriteID = PicID[nIndex]; UISpriteInfo sp = CAtlasMng.instance.GetSafeSpriteByID(nSpriteID); nWidth = (int)sp.outer.width; nHeight = (int)sp.outer.height; nMaxWidth += nWidth + nCharGap; if (nMaxHeight < nHeight) { nMaxHeight = nHeight; } HUDVertex v = null; if (nStart < m_aSprite.size) { v = m_aSprite[nStart]; if (v.AtlasID != sp.m_nAtlasID) { if (v.hudMesh != null) { v.hudMesh.EraseHUDVertex(v); } v.hudMesh = null; v.AtlasID = sp.m_nAtlasID; } v.SpriteID = nSpriteID; v.Offset.Set(nX, titleAttrib.SpriteOffsetY); v.InitSprite(nWidth, nHeight); } else { v = PushSprite(nSpriteID, nWidth, nHeight, nX, titleAttrib.SpriteOffsetY); } v.Scale = m_fScale; if (v.hudMesh == null) { if (m_bInitHUDMesh) { v.hudMesh = m_pBatcher.m_MeshRender.QueryMesh(sp.m_nAtlasID); v.hudMesh.PushHUDVertex(v); } } else { v.hudMesh.VertexDirty(); } nX = nMaxWidth; ++nStart; } title.m_nEnd = nStart; // 删除多余的 for (--nEnd; nEnd >= nStart; --nEnd) { HUDVertex v = m_aSprite[nEnd]; if (v.hudMesh != null) { v.hudMesh.EraseHUDVertex(v); } v.hudMesh = null; m_aSprite.RemoveAt(nEnd); HUDVertex.ReleaseVertex(v); } nMaxWidth -= nCharGap; title.m_fWidth = nMaxWidth; title.m_nHeight = nMaxHeight; nMaxHeight += titleAttrib.SpriteReduceHeight; m_fCurLineWidth = nMaxWidth; m_fCurLineHeight = nMaxHeight; }
void DrawEditData() { Color blue = new Color(0f, 0.7f, 1f, 1f); Color green = new Color(0.4f, 1f, 0f, 1f); // 绘制预览面板 if (mSprite == null || !mSprite.isValid) { return; } if (m_sprite == null) { return; } UISpriteInfo sprite = AtlasMng_Editor.instance.GetSprite(mSprite.name); if (sprite == null) { return; } UITexAtlas atlas = AtlasMng_Editor.instance.GetAltasBySpriteName(mSprite.name); if (atlas == null) { return; } Rect inner = sprite.inner; Rect outer = sprite.outer; if (atlas.coordinates == UITexAtlas.Coordinates.Pixels) { GUI.backgroundColor = green; // 渲染编辑对象 outer = HUDEditorTools.IntRect("Dimensions", sprite.outer); Vector4 border = new Vector4( sprite.inner.xMin - sprite.outer.xMin, sprite.inner.yMin - sprite.outer.yMin, sprite.outer.xMax - sprite.inner.xMax, sprite.outer.yMax - sprite.inner.yMax); // 渲染编辑对象 GUI.backgroundColor = blue; border = HUDEditorTools.IntPadding("Border", border); GUI.backgroundColor = Color.white; inner.xMin = sprite.outer.xMin + border.x; inner.yMin = sprite.outer.yMin + border.y; inner.xMax = sprite.outer.xMax - border.z; inner.yMax = sprite.outer.yMax - border.w; } else { // Draw the inner and outer rectangle dimensions GUI.backgroundColor = green; outer = EditorGUILayout.RectField("Outer Rect", sprite.outer); GUI.backgroundColor = blue; inner = EditorGUILayout.RectField("Inner Rect", sprite.inner); GUI.backgroundColor = Color.white; } if (outer.xMax < outer.xMin) { outer.xMax = outer.xMin; } if (outer.yMax < outer.yMin) { outer.yMax = outer.yMin; } if (outer != sprite.outer) { float x = outer.xMin - sprite.outer.xMin; float y = outer.yMin - sprite.outer.yMin; inner.x += x; inner.y += y; } // Sanity checks to ensure that the inner rect is always inside the outer inner.xMin = Mathf.Clamp(inner.xMin, outer.xMin, outer.xMax); inner.xMax = Mathf.Clamp(inner.xMax, outer.xMin, outer.xMax); inner.yMin = Mathf.Clamp(inner.yMin, outer.yMin, outer.yMax); inner.yMax = Mathf.Clamp(inner.yMax, outer.yMin, outer.yMax); bool changed = false; if (sprite.inner != inner || sprite.outer != outer) { sprite.inner = inner; sprite.outer = outer; MarkSpriteAsDirty(); // 应用修改 changed = true; m_bDirty = true; } EditorGUILayout.Separator(); if (atlas.coordinates == UITexAtlas.Coordinates.Pixels) { int left = Mathf.RoundToInt(sprite.paddingLeft * sprite.outer.width); int right = Mathf.RoundToInt(sprite.paddingRight * sprite.outer.width); int top = Mathf.RoundToInt(sprite.paddingTop * sprite.outer.height); int bottom = Mathf.RoundToInt(sprite.paddingBottom * sprite.outer.height); HUDEditorTools.IntVector a = HUDEditorTools.IntPair("Padding", "Left", "Top", left, top); HUDEditorTools.IntVector b = HUDEditorTools.IntPair(null, "Right", "Bottom", right, bottom); if (changed || a.x != left || a.y != top || b.x != right || b.y != bottom) { sprite.paddingLeft = a.x / sprite.outer.width; sprite.paddingTop = a.y / sprite.outer.height; sprite.paddingRight = b.x / sprite.outer.width; sprite.paddingBottom = b.y / sprite.outer.height; MarkSpriteAsDirty(); // 应用修改 m_bDirty = true; changed = true; } } else { // Create a button that can make the coordinates pixel-perfect on click GUILayout.BeginHorizontal(); { GUILayout.Label("Correction", GUILayout.Width(75f)); Rect corrected0 = outer; Rect corrected1 = inner; if (atlas.coordinates == UITexAtlas.Coordinates.Pixels) { corrected0 = HUDMath.MakePixelPerfect(corrected0); corrected1 = HUDMath.MakePixelPerfect(corrected1); } else { corrected0 = HUDMath.MakePixelPerfect(corrected0, atlas.texWidth, atlas.texHeight); corrected1 = HUDMath.MakePixelPerfect(corrected1, atlas.texWidth, atlas.texHeight); } if (corrected0 == sprite.outer && corrected1 == sprite.inner) { GUI.color = Color.grey; GUILayout.Button("Make Pixel-Perfect"); GUI.color = Color.white; } else if (GUILayout.Button("Make Pixel-Perfect")) { outer = corrected0; inner = corrected1; GUI.changed = true; m_bDirty = true; changed = true; } } GUILayout.EndHorizontal(); } if (changed) { m_bDirty = IsChangeSprite(m_sprite, sprite); } HUDEditorTools.DrawSeparator(); GUILayout.BeginHorizontal(); GUI.backgroundColor = m_bDirty ? Color.green : Color.white; if (GUILayout.Button("修改")) { if (m_bDirty && m_sprite != null) { m_sprite.Copy(sprite); MarkSpriteAsDirty(); // 应用修改 AtlasMng_Editor.instance.SaveAltasCfg(); m_bDirty = false; } } if (GUILayout.Button("撤消")) { sprite.Copy(m_sprite); MarkSpriteAsDirty(); // 应用修改 m_bDirty = false; } GUI.backgroundColor = Color.white; GUILayout.EndHorizontal(); if (HUDEditorTools.previousSelection != null) { //NGUIEditorTools.DrawSeparator(); //GUI.backgroundColor = Color.green; //if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name)) //{ // NGUIEditorTools.SelectPrevious(); //} //GUI.backgroundColor = Color.white; } }
protected void SlicedFill(int nSpriteID, int nWidth, int nHeight, int nStart, float fBloodPos) { UISpriteInfo sp = CAtlasMng.instance.GetSafeSpriteByID(nSpriteID); if (sp == null) { return; } if (fBloodPos < 0.0) { fBloodPos = 0.0f; } if (fBloodPos > 1.0f) { fBloodPos = 1.0f; } int nBloodWidth = (int)(nWidth * fBloodPos + 0.5f); int nAtlasID = sp.m_nAtlasID; Rect mOuterUV = sp.outer; Rect mInnerUV = sp.inner; int nOuterW = (int)(mOuterUV.width + 0.5f); int nOuterH = (int)(mOuterUV.height + 0.5f); //int nInnerW = (int)(mInnerUV.width + 0.5f); //int nInnerH = (int)(mInnerUV.height + 0.5f); int nW1 = (int)(mInnerUV.xMin - mOuterUV.xMin + 0.5f); int nH1 = (int)(mInnerUV.yMin - mOuterUV.yMin + 0.5f); int nW2 = (int)(mOuterUV.xMax - mInnerUV.xMax + 0.5f); int nH2 = (int)(mOuterUV.yMax - mInnerUV.yMax + 0.5f); UITexAtlas texAtlas = CAtlasMng.instance.GetAtlasByID(sp.m_nAtlasID); if (texAtlas != null && texAtlas.coordinates == UITexAtlas.Coordinates.Pixels) { mOuterUV = HUDVertex.ConvertToTexCoords(mOuterUV, texAtlas.texWidth, texAtlas.texHeight); mInnerUV = HUDVertex.ConvertToTexCoords(mInnerUV, texAtlas.texWidth, texAtlas.texHeight); } if (nOuterW > 0 && nW1 + nW2 > nBloodWidth) { nW1 = nBloodWidth * nW1 / nOuterW; nW2 = nBloodWidth - nW1; } if (nOuterH > 0 && nH1 + nH2 > nHeight) { nH1 = nHeight * nH1 / nOuterH; nH2 = nHeight - nH1; } //if (nOuterW > 0) //{ // nW1 = nW1 * nBloodWidth / nOuterW; // nW2 = nW2 * nBloodWidth / nOuterW; //} //if (nOuterH > 0) //{ // nH1 = nH1 * nHeight / nOuterH; // nH2 = nH2 * nHeight / nOuterH; //} int nMW = nBloodWidth - nW1 - nW2; int nMH = nHeight - nH1 - nH2; HUDVertex v0 = m_aSprite[nStart]; HUDVertex v1 = m_aSprite[nStart + 1]; HUDVertex v2 = m_aSprite[nStart + 2]; HUDVertex v3 = m_aSprite[nStart + 3]; HUDVertex v4 = m_aSprite[nStart + 4]; HUDVertex v5 = m_aSprite[nStart + 5]; HUDVertex v6 = m_aSprite[nStart + 6]; HUDVertex v7 = m_aSprite[nStart + 7]; HUDVertex v8 = m_aSprite[nStart + 8]; // (mOuterUV.xMin, mOuterUV.yMin) // // (mInnerUV.xMin, mInnerUV.yMin) // (mInnerUV.yMin, mInnerUV.yMax) // // (mOuterUV.xMax, mOuterUV.yMax) // a6 a7 a8 a0 a1 a2 // a3 a4 a5 ==> a3 a4 a5 // a0 a1 a2 a6 a7 a8 float fIn_xMin = mInnerUV.xMin; float fIn_xMax = mInnerUV.xMax; float fIn_yMin = mInnerUV.yMin; float fIn_yMax = mInnerUV.yMax; float fOu_xMin = mOuterUV.xMin; float fOu_xMax = mOuterUV.xMax; float fOu_yMin = mOuterUV.yMin; float fOu_yMax = mOuterUV.yMax; float fX2 = nW1 + nMW; float fY2 = nH2 + nMH; v0.SlicedFill(nW1, nH2, 0f, 0f, fOu_xMin, fOu_yMin, fIn_xMin, fIn_yMin); v1.SlicedFill(nMW, nH2, nW1, 0f, fIn_xMin, fOu_yMin, fIn_xMax, fIn_yMin); v2.SlicedFill(nW2, nH2, fX2, 0f, fIn_xMax, fOu_yMin, fOu_xMax, fIn_yMin); v3.SlicedFill(nW1, nMH, 0f, nH2, fOu_xMin, fIn_yMin, fIn_xMin, fIn_yMax); v4.SlicedFill(nMW, nMH, nW1, nH2, fIn_xMin, fIn_yMin, fIn_xMax, fIn_yMax); v5.SlicedFill(nW2, nMH, fX2, nH2, fIn_xMax, fIn_yMin, fOu_xMax, fIn_yMax); v6.SlicedFill(nW1, nH1, 0f, fY2, fOu_xMin, fIn_yMax, fIn_xMin, fOu_yMax); v7.SlicedFill(nMW, nH1, nW1, fY2, fIn_xMin, fIn_yMax, fIn_xMax, fOu_yMax); v8.SlicedFill(nW2, nH1, fX2, fY2, fIn_xMax, fIn_yMax, fOu_xMax, fOu_yMax); v0.Scale = m_fScale; v1.Scale = m_fScale; v2.Scale = m_fScale; v3.Scale = m_fScale; v4.Scale = m_fScale; v5.Scale = m_fScale; v6.Scale = m_fScale; v7.Scale = m_fScale; v8.Scale = m_fScale; }