/// <summary> /// Sprites's custom properties based on the type. /// </summary> override protected void DrawExtraProperties() { NGUIEditorTools.DrawSeparator(); if (GetType() == typeof(NGUISpriteInspector)) { //GUILayout.BeginHorizontal(); NGUISprite.Type type = (NGUISprite.Type)EditorGUILayout.EnumPopup("Sprite Type", mSprite.type); //GUILayout.Label("sprite", GUILayout.Width(58f)); //GUILayout.EndHorizontal(); if (mSprite.type != type) { NGUIEditorTools.RegisterUndo("Sprite Change", mSprite); mSprite.type = type; EditorUtility.SetDirty(mSprite.gameObject); } } if (mSprite.type == NGUISprite.Type.Sliced || mSprite.type == NGUISprite.Type.MirrorSliced) { bool fill = EditorGUILayout.Toggle("Fill Center", mSprite.fillCenter); if (mSprite.fillCenter != fill) { NGUIEditorTools.RegisterUndo("Sprite Change", mSprite); mSprite.fillCenter = fill; EditorUtility.SetDirty(mSprite.gameObject); } if (mSprite.type == NGUISprite.Type.MirrorSliced) { bool reverse = EditorGUILayout.Toggle("Reverse", mSprite.fillReverse); if (mSprite.fillReverse != reverse) { NGUIEditorTools.RegisterUndo("Sprite Change", mSprite); mSprite.fillReverse = reverse; EditorUtility.SetDirty(mSprite.gameObject); } } } else if (mSprite.type == NGUISprite.Type.MirrorSimple) { bool reverse = EditorGUILayout.Toggle("Reverse", mSprite.fillReverse); if (mSprite.fillReverse != reverse) { NGUIEditorTools.RegisterUndo("Sprite Change", mSprite); mSprite.fillReverse = reverse; EditorUtility.SetDirty(mSprite.gameObject); } } else if (mSprite.type == NGUISprite.Type.Filled) { if ((int)mSprite.fillDirection > (int)NGUISprite.FillDirection.Radial360) { mSprite.fillDirection = NGUISprite.FillDirection.Horizontal; EditorUtility.SetDirty(mSprite); } NGUISprite.FillDirection fillDirection = (NGUISprite.FillDirection)EditorGUILayout.EnumPopup("Fill Dir", mSprite.fillDirection); float fillAmount = EditorGUILayout.Slider("Fill Amount", mSprite.fillAmount, 0f, 1f); bool invert = EditorGUILayout.Toggle("Invert Fill", mSprite.invert); if (mSprite.fillDirection != fillDirection || mSprite.fillAmount != fillAmount || mSprite.invert != invert) { NGUIEditorTools.RegisterUndo("Sprite Change", mSprite); mSprite.fillDirection = fillDirection; mSprite.fillAmount = fillAmount; mSprite.invert = invert; EditorUtility.SetDirty(mSprite); } } GUILayout.Space(4f); }
/// <summary> /// Adjust the scale of the widget to make it pixel-perfect. /// </summary> override public void MakePixelPerfect() { if (!isValid) { return; } UpdateUVs(false); NGUISprite.Type t = type; if (t == Type.Sliced || t == Type.MirrorSliced) { // Sliced sprite should have dimensions divisible by 2 for best results Vector3 pos = cachedTransform.localPosition; pos.x = Mathf.RoundToInt(pos.x); pos.y = Mathf.RoundToInt(pos.y); pos.z = Mathf.RoundToInt(pos.z); cachedTransform.localPosition = pos; Vector3 scale = cachedTransform.localScale; scale.x = Mathf.RoundToInt(scale.x * 0.5f) << 1; scale.y = Mathf.RoundToInt(scale.y * 0.5f) << 1; scale.z = 1f; cachedTransform.localScale = scale; } else if (t == Type.Tiled) { // Tiled sprite just needs whole integers Vector3 pos = cachedTransform.localPosition; pos.x = Mathf.RoundToInt(pos.x); pos.y = Mathf.RoundToInt(pos.y); pos.z = Mathf.RoundToInt(pos.z); cachedTransform.localPosition = pos; Vector3 scale = cachedTransform.localScale; scale.x = Mathf.RoundToInt(scale.x); scale.y = Mathf.RoundToInt(scale.y); scale.z = 1f; cachedTransform.localScale = scale; } else { // Other sprites should assume the original dimensions of the sprite Texture tex = mainTexture; Vector3 scale = cachedTransform.localScale; if (tex != null) { Rect rect = NGUIMath.ConvertToPixels(outerUV, tex.width, tex.height, true); float pixelSize = atlas.pixelSize; scale.x = Mathf.RoundToInt(rect.width * pixelSize) * Mathf.Sign(scale.x); scale.y = Mathf.RoundToInt(rect.height * pixelSize) * Mathf.Sign(scale.y); scale.z = 1f; if (t == Type.MirrorSimple) { scale.x *= 2; } cachedTransform.localScale = scale; } int width = Mathf.RoundToInt(Mathf.Abs(scale.x) * (1f + mSprite.paddingLeft + mSprite.paddingRight)); int height = Mathf.RoundToInt(Mathf.Abs(scale.y) * (1f + mSprite.paddingTop + mSprite.paddingBottom)); Vector3 pos = cachedTransform.localPosition; pos.x = (Mathf.CeilToInt(pos.x * 4f) >> 2); pos.y = (Mathf.CeilToInt(pos.y * 4f) >> 2); pos.z = Mathf.RoundToInt(pos.z); if (width % 2 == 1 && (pivot == Pivot.Top || pivot == Pivot.Center || pivot == Pivot.Bottom)) { pos.x += 0.5f; } if (height % 2 == 1 && (pivot == Pivot.Left || pivot == Pivot.Center || pivot == Pivot.Right)) { pos.y += 0.5f; } cachedTransform.localPosition = pos; } }