Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - WinMain

Pages: [1]
1
NGUI 3 Support / Re: How to apply a shader effect on one sprite?
« on: January 05, 2015, 11:27:01 PM »
See UIShaderSprite for the working copy of this thread.

You can find it on the following github repository:

https://github.com/kimsama/Unity-NGUIExtension

As my experiments, it needs to call panel.RebuildAllDrawCalls() to correctly apply a new shader which is differenct to what NGUI applies in UIDrawCall.UpdateMaterial() call.
Awesome,Thank you so much.

2
NGUI 3 Support / Re: How to apply a shader effect on one sprite?
« on: November 04, 2014, 04:53:39 AM »
thanks for your solution dilshod.

3
NGUI 3 Support / Re: How to apply a shader effect on one sprite?
« on: October 27, 2014, 10:48:55 PM »
Here is my version of how it could be done. i'm going to use this in my current project.

UIShaderSprite.cs:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class UIShaderSprite : UISprite {
  6.         Material mMaterial;
  7.         [HideInInspector][SerializeField] Shader mShader;
  8.  
  9.         // cache materials
  10.         static Dictionary<int, Dictionary<int, Material>> atlasMaterials = new Dictionary<int, Dictionary<int, Material>>();
  11.  
  12.         public override Material material {
  13.                 get {
  14.                         if (mShader != null) {
  15.                                 if (mMaterial == null || mChanged) {
  16.                                         Dictionary<int, Material> shaderMaterials;
  17.                                         if (!atlasMaterials.TryGetValue(atlas.GetInstanceID(), out shaderMaterials))
  18.                                                 atlasMaterials[atlas.GetInstanceID()] = shaderMaterials = new Dictionary<int, Material>();
  19.  
  20.                                         if (!shaderMaterials.TryGetValue(mShader.GetInstanceID(), out mMaterial) || mMaterial == null)
  21.                                                 shaderMaterials[mShader.GetInstanceID()] = mMaterial = new Material(base.material) {shader = mShader};
  22.                                 }
  23.                                 return mMaterial;
  24.                         }
  25.                         return base.material;
  26.                 }
  27.         }
  28. }
  29.  

Editor/UIShaderSpriteInspector.cs:
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. [CanEditMultipleObjects]
  6. [CustomEditor(typeof(UIShaderSprite), true)]
  7. public class UIShaderSpriteInspector : UISpriteInspector {
  8.  
  9.         protected override bool ShouldDrawProperties() {
  10.                 NGUIEditorTools.DrawProperty("Shader", serializedObject, "mShader", GUILayout.MinWidth(20f));
  11.                 return base.ShouldDrawProperties();
  12.         }
  13. }
  14.  
Thank you for your idea.But I have one question is,whether this way will make the drawcall rise?Becase if there are too many sprites in atlas,there will be too many materials.

4
NGUI 3 Support / How to apply a shader effect on one sprite?
« on: October 27, 2014, 05:24:13 AM »
I know how to apply the shader on a material,but I just want to apply a effect on one sprite in atlas.It's there some way to do this?

5
Hi,guys,unfortune,my project using NGUI 2.0.3,now we want to make UILabel support hyperlike.so I modify my UIFont code to implement it like NGUI 3.X.I use mouse position(screen coordinate system) to judge if touch the link.The code like below:
UILabel:
public int GetCharacterIndexAtPosition(Vector3 sceenPos)
{
        return mFont.GetExactCharacterIndex(sceenPos);
}
UIFont:
if UNITY_3_5_4
   public void Print (string text, Color color, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color> cols,
      bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth)
#else
   public void Print (string text, Color32 color, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color> cols,
      bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth)
#endif
   {
      if (mReplacement != null)
      {
         mReplacement.Print(text, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth);
      }
      else if (mFont != null && text != null)
      {
         if (!mFont.isValid && !UseDynamicFont)
         {
            UIDebug.LogError("Attempting to print using an invalid font!");
            return;
         }
           
            mTempIndices.Clear();
            mTempVerts.Clear();
            linkTextIdx.Clear();
         mColors.Clear();
         mColors.Add(color);

         Vector2 scale = charSize > 0 ? new Vector2(1f / charSize, 1f / charSize) : Vector2.one;

         int indexOffset = verts.size;
         int maxX = 0;
         int x = 0;
         int y = 0;
         int prev = 0;
         int lineHeight = (charSize + mSpacingY);
         Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
         Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
         float invX = uvRect.width / mFont.texWidth;
         float invY = mUVRect.height / mFont.texHeight;
         int textLength = text.Length;
         
         //int vertsIndex = 0;
         float yMax_row = 0;
         
          //JUDIVA, include symbols in font
            RegisterFont(this);
            if (UseDynamicFont)
         {
            CharacterInfo charInfo;
            dynamicFont.RequestCharactersInTexture("|",dynamicFontSize,dynamicFontStyle);
            if (dynamicFont.GetCharacterInfo('|', out charInfo, dynamicFontSize, dynamicFontStyle))
               {                                  
               yMax_row = Mathf.Abs( Mathf.RoundToInt(charInfo.vert.yMax) );
               lineHeight =  Mathf.Abs(Mathf.RoundToInt(charInfo.vert.height)) + mSpacingY;
               }
            dynamicFont.RequestCharactersInTexture(text, dynamicFontSize, dynamicFontStyle);
         }

            int nLinkStart = -1;
            string link = "";
         for (int i = 0; i < textLength; ++i)
         {
            char c = text;

            if (c == '\n')
            {
               if (x > maxX) maxX = x;

               if (alignment != Alignment.Left)
               {
                  Align(verts, indexOffset, alignment, x, lineWidth);
                  indexOffset = verts.size;
               }

               x = 0;
               y += lineHeight;
               prev = 0;
               
               /*
               for(int j = vertsIndex;j < verts.size; ++j)
               {
                  Vector2 temp = verts.buffer[j];
                  temp.y += (yMax_row/2 - charSize/2)*scale.y;
                  verts.buffer[j] = temp;
               }
               yMax_row = 0;
               vertsIndex = verts.size;*/
               continue;
            }

            if (c < ' ')
            {
               prev = 0;
               continue;
            }

            if (encoding && c == '[')
            {
               int retVal = NGUITools.ParseSymbol(text, i, mColors);

               if (retVal > 0)
               {
                  color = mColors[mColors.Count - 1];
                  i += retVal - 1;
                  continue;
               }
            }

                // ext code
                // HyperLink
                if (encoding && c == '#')
                {
                    int retVal = 0;
                    NGUITools.HyperLinkFlag flag = NGUITools.ParseHyperLink(text, i, ref retVal, ref link);

                    if (retVal > 0)
                    {
                        if (flag == NGUITools.HyperLinkFlag.EHead)
                        {
                            i += retVal - 1;
                            nLinkStart = i;
                        }
                        else if(flag == NGUITools.HyperLinkFlag.EEnd && nLinkStart != -1)
                        {
                            for (int j = nLinkStart; j < i-1; ++j)
                            {
                                linkTextIdx.Add(j, link);
                            }

                            i += retVal - 1;
                        }

                        continue;
                    }
                }

                if (!UseDynamicFont)
                {
                    // See if there is a symbol matching this text
                   
                    {
                        BMGlyph glyph = mFont.GetGlyph(c);
                        if (glyph == null) continue;

                        if (prev != 0) x += glyph.GetKerning(prev);

                        if (c == ' ')
                        {
                            x += mSpacingX + glyph.advance;
                            prev = c;
                            continue;
                        }

                        v0.x = scale.x * (x + glyph.offsetX);
                        v0.y = -scale.y * (y + glyph.offsetY);

                        v1.x = v0.x + scale.x * glyph.width;
                        v1.y = v0.y - scale.y * glyph.height;

                        u0.x = mUVRect.xMin + invX * glyph.x;
                        u0.y = mUVRect.yMax - invY * glyph.y;

                        u1.x = u0.x + invX * glyph.width;
                        u1.y = u0.y - invY * glyph.height;


                        // ext code
                        mTempIndices.Add(i);
                        mTempVerts.Add(new Vector3(x, y, 0));


                        x += mSpacingX + glyph.advance;
                        prev = c;

                        if (true)
                        {
                            for (int b = 0; b < 4; ++b) cols.Add(color);
                        }
                       
                    }
               
                    uvs.Add(new Vector2(u1.x, u0.y));
                    uvs.Add(new Vector2(u1.x, u1.y));
                    uvs.Add(new Vector2(u0.x, u1.y));
                    uvs.Add(new Vector2(u0.x, u0.y));

                    verts.Add(new Vector3(v1.x, v0.y));
                    verts.Add(new Vector3(v1.x, v1.y));
                    verts.Add(new Vector3(v0.x, v1.y));
                    verts.Add(new Vector3(v0.x, v0.y));

                    // ext code
                    mTempVerts.Add(new Vector3(x, y + lineHeight, 0));

                }

                else
                {
                    //v0 v1 are the two corners
                    CharacterInfo charInfo;
                    if (!dynamicFont.GetCharacterInfo(c, out charInfo, dynamicFontSize, dynamicFontStyle))
                    {
                        UIDebug.LogError("character not found in font");
                        continue;
                    }
               
                    v0.x = scale.x * (x + charInfo.vert.xMin);
                    v0.y = scale.y * (-y + charInfo.vert.yMax);
                    v1.x = scale.x * (x + charInfo.vert.xMax);
                    v1.y = scale.y * (-y + charInfo.vert.yMin);
               

                   
                    u0.x = charInfo.uv.xMin;
                    u0.y = charInfo.uv.yMin;
                    u1.x = charInfo.uv.xMax;
                    u1.y = charInfo.uv.yMax;

                    // ext code
                    mTempIndices.Add(i);
                    mTempVerts.Add(new Vector3(x, y, 0));


                    x += mSpacingX+(int)charInfo.width;

                    for (int b = 0; b < 4; ++b) cols.Add(color);

                    if (charInfo.flipped)
                    {
                        //swap entries
                        uvs.Add(new Vector2(u0.x, u1.y));
                        uvs.Add(new Vector2(u0.x, u0.y));
                        uvs.Add(new Vector2(u1.x, u0.y));
                        uvs.Add(new Vector2(u1.x, u1.y));
                    }
                    else
                    {
                        uvs.Add(new Vector2(u1.x, u0.y));
                        uvs.Add(new Vector2(u0.x, u0.y));
                        uvs.Add(new Vector2(u0.x, u1.y));
                        uvs.Add(new Vector2(u1.x, u1.y));
                    }

                    verts.Add(new Vector3(v1.x, v0.y));
                    verts.Add(new Vector3(v0.x, v0.y));
                    verts.Add(new Vector3(v0.x, v1.y));
                    verts.Add(new Vector3(v1.x, v1.y));

                    // ext code
                    mTempVerts.Add(new Vector3(x, y + lineHeight, 0));

                }
         }
         
         if(UseDynamicFont)
         {            
            for(int j = 0;j < verts.size; ++j)
            {
               Vector2 temp = verts.buffer[j];
               temp.y += (yMax_row/2 - charSize/2)*scale.y;
               verts.buffer[j] = temp;
            }
         }
         
         if (alignment != Alignment.Left && indexOffset < verts.size)
         {
                if (!UseDynamicFont)
                    Align(verts, indexOffset, alignment, x, lineWidth);
                else
            {
               Align(verts, indexOffset, alignment, x, lineWidth);
            }
                   
            indexOffset = verts.size;
         }
      }
   }

The code where comment "ext code" is my code.That I want to put the real pixels the font rendering size into mTempVerts,and use GetCharacterIndexAtPosition() to judge the mouse position whether click on hyperlink.But now the value in mTempVerts is wrong,it's not the right value on the screen,example the value in mTempVerts is 32,but on the screen it's more than 10.I think maybe I need do same scale in my value.So how I can to correct my code.

6
NGUI 3 Support / clip problem in UIPanel with stretch
« on: July 29, 2012, 11:08:26 PM »
hi,our game use 1024*768 resolution in IPAD,that no problem,but now we want to use out layout in android's pad device,there resolution ratio are not same,like 1280*800 1280*600 and so on.We don't want to make many size layouts and images for so many resolution,we just want to stretch them,so I modify the UIRoot.cs like this:
  1. manualHeight = Mathf.Max(2, Screen.height);
  2.                 //manualWidth = Mathf.Max(2, Screen.width);
  3.  
  4.                 float size = 2f / manualHeight * ((float)manualHeight / 768);
  5.                 float sizeX = (float)Screen.width / 1024 * 2f / manualHeight;
  6.                 Vector3 ls = mTrans.localScale;
  7.  
  8.                 if (!Mathf.Approximately(ls.x, sizeX) ||
  9.                         !Mathf.Approximately(ls.y, size) ||
  10.                         !Mathf.Approximately(ls.z, size))
  11.                 {
  12.                         mTrans.localScale = new Vector3(sizeX, size, size);
  13.                 }
en,it worked fine as I want,but all my drag list in uipanel are wrong,the cliprect is not correct,their children out the clip rect and still visible,how can I do to make it correct.I use NGUI ver1.82.Waitting for help,thank you.

Pages: [1]