Author Topic: Cropping and Offset of Simple Texture  (Read 13537 times)

alosh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Cropping and Offset of Simple Texture
« on: August 01, 2012, 03:23:39 AM »
Hey there,

Is there a way to crop or set the offset of a simple texture in nGUI?

Scenario:
I need to download a full-body image/texture of a player character from our server using WWW object. Now I need to crop the downloaded texture and adjust its offset to show only the face of the character and part of the upper chest instead of the whole body.

Is there a way this can accomplished in nGUI?
« Last Edit: August 01, 2012, 04:14:14 AM by alosh »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Cropping and Offset of Simple Texture
« Reply #1 on: August 01, 2012, 04:24:34 AM »
In 2.1.2, nope. It's a feature that I've added a couple of days ago, so you'll see it in the next release (within a few days).

simon129

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 20
    • View Profile
Re: Cropping and Offset of Simple Texture
« Reply #2 on: August 02, 2012, 09:36:04 PM »
I made my own UITextureUV based on UITexture.

UITextureUV.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/UI/TextureUV")]
  6. public class UITextureUV : UITexture
  7. {
  8.     public Rect UV = new Rect(0, 0, 1, 1);
  9.  
  10.     override public void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color> cols)
  11.     {
  12.         Vector2 uv0 = new Vector2(UV.xMin, UV.yMin);
  13.         Vector2 uv1 = new Vector2(UV.xMax, UV.yMax);
  14.  
  15.         verts.Add(new Vector3(1f, 0f, 0f));
  16.         verts.Add(new Vector3(1f, -1f, 0f));
  17.         verts.Add(new Vector3(0f, -1f, 0f));
  18.         verts.Add(new Vector3(0f, 0f, 0f));
  19.  
  20.         uvs.Add(uv1);
  21.         uvs.Add(new Vector2(uv1.x, uv0.y));
  22.         uvs.Add(uv0);
  23.         uvs.Add(new Vector2(uv0.x, uv1.y));
  24.  
  25.         cols.Add(color);
  26.         cols.Add(color);
  27.         cols.Add(color);
  28.         cols.Add(color);
  29.     }
  30. }
  31.  


UITextureUVInspector.cs
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4.  
  5. /// <summary>
  6. /// Inspector class used to edit UITextureUV.
  7. /// </summary>
  8.  
  9. [CustomEditor(typeof(UITextureUV))]
  10. public class UITextureUVInspector : UITextureInspector
  11. {
  12.     private Rect mUV;
  13.  
  14.     public override void OnInspectorGUI()
  15.     {
  16.         UITextureUV texture = target as UITextureUV;
  17.        
  18.         GUILayout.BeginHorizontal();
  19.         GUILayout.Space(40f);
  20.         mUV = EditorGUILayout.RectField("UV", texture.UV);
  21.         GUILayout.EndHorizontal();
  22.  
  23.         texture.UV = mUV;
  24.  
  25.         base.OnInspectorGUI();
  26.     }
  27.  
  28.     override protected void OnDrawTexture()
  29.     {
  30.         Texture2D tex = mWidget.mainTexture as Texture2D;
  31.  
  32.         if (tex != null)
  33.         {
  34.             // Draw the atlas
  35.             EditorGUILayout.Separator();
  36.             Rect r = NGUIEditorTools.DrawSprite(tex, new Rect(0F, 0F, 1F, 1F), null);
  37.  
  38.             // Draw the selection
  39.             NGUIEditorTools.DrawOutline(r, mUV, new Color(0.4f, 1f, 0f, 1f));
  40.  
  41.             // Sprite size label
  42.             Rect rect = GUILayoutUtility.GetRect(Screen.width, 18f);
  43.             EditorGUI.DropShadowLabel(rect, "Texture Size: " + tex.width + "x" + tex.height);
  44.         }
  45.     }
  46. }
  47.  

alosh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Cropping and Offset of Simple Texture
« Reply #3 on: August 12, 2012, 03:28:53 AM »
I forgot to update this in time:
I've worked around this by placing the simple texture in a panel and enabled clipping.
Moving the sprite inside emulated offset and changing the clipping size emulated cropping. It worked quite well for me.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Cropping and Offset of Simple Texture
« Reply #4 on: August 12, 2012, 03:52:49 AM »
Putting it in a Panel will sadly create an extra draw call for you.

What I did was create a tiledsprite and set the parameters to less than the sprite's size to create clipping. Not sure if that will work for you, but it worked for me. :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Cropping and Offset of Simple Texture
« Reply #5 on: August 12, 2012, 04:56:52 AM »
Either approach is moot right now since UITexture has a UV rect you can specify as of 2.1.4.