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.


Topics - vexe

Pages: 1 2 [3] 4
31
Hello,

I just hacked (modified) TypeWriterEffect to not destroy itself when it finishes. Instead, there's a boolean 'Begin' that you set to true each time you want the effect to occur.

First, I tried making a 'Begin' method, and forget about Update:
  1.         public void Begin()
  2.         {
  3.                 if (mLabel == null)
  4.                 {
  5.                         mLabel = GetComponent<UILabel>();
  6.                         mLabel.supportEncoding = false;
  7.                         mLabel.symbolStyle = UIFont.SymbolStyle.None;
  8.                         mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth / mLabel.cachedTransform.localScale.x,
  9.                         mLabel.maxLineCount, false, UIFont.SymbolStyle.None);
  10.                 }
  11.  
  12.                 while (mOffset < mText.Length)
  13.                 {
  14.                         if (mNextChar <= Time.time)
  15.                         {
  16.                                 charsPerSecond = Mathf.Max(1, charsPerSecond);
  17.  
  18.                                 // Periods and end-of-line characters should pause for a longer time.
  19.                                 float delay = 1f / charsPerSecond;
  20.                                 char c = mText[mOffset];
  21.                                 if (c == '.' || c == '\n' || c == '!' || c == '?') delay *= 4f;
  22.  
  23.                                 mNextChar = Time.time + delay;
  24.                                 mLabel.text = mText.Substring(0, ++mOffset);
  25.                         }
  26.                 }
  27.         }
  28.  

The problem with that, is that it gets stuck between the condition of the while and the if (infinite loop) because the method is not being called in Update, I guess...

So I thought I should use update, so I came up with this idea of using a boolean Begin, set it to true each time I want the effect to happen:

  1.         private bool begin;
  2.         public bool Begin {get {return begin;} set{begin = value; Reset();}}
  3.  
  4.         private void Reset()
  5.         {
  6.                 mOffset = 0;
  7.                 mNextChar = 0;
  8.                 if (mLabel != null)
  9.                         mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth / mLabel.cachedTransform.localScale.x,
  10.                                     mLabel.maxLineCount, false, UIFont.SymbolStyle.None);
  11.         }
  12.  
  13.         void Update()
  14.         {
  15.                 if (begin)
  16.                 {
  17.                         if (mLabel == null)
  18.                         {
  19.                                 mLabel = GetComponent<UILabel>();
  20.                                 mLabel.supportEncoding = false;
  21.                                 mLabel.symbolStyle = UIFont.SymbolStyle.None;
  22.                                 mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth/mLabel.cachedTransform.localScale.x,
  23.                                             mLabel.maxLineCount, false, UIFont.SymbolStyle.None);
  24.                         }
  25.  
  26.                         if (mOffset < mText.Length)
  27.                         {
  28.                                 if (mNextChar <= Time.time)
  29.                                 {
  30.                                         charsPerSecond = Mathf.Max(1, charsPerSecond);
  31.  
  32.                                         // Periods and end-of-line characters should pause for a longer time.
  33.                                         float delay = 1f/charsPerSecond;
  34.                                         char c = mText[mOffset];
  35.                                         if (c == '.' || c == '\n' || c == '!' || c == '?') delay *= 4f;
  36.  
  37.                                         mNextChar = Time.time + delay;
  38.                                         mLabel.text = mText.Substring(0, ++mOffset);
  39.                                 }
  40.                         }
  41.                 }//             else Destroy(this);
  42.                 else begin = false;
  43.         }
  44.  

Is this good? is there a better way you might suggest? I would love to not use Update, and use a separate method like in my first try - Thanks.

32
Hello,

in my inventory system, I want to be able to determine the number of rows/columns I have based on the height/width on my inventory background and the slots size. (See attachment)

So like, if I had a 500x500 background, with a slot size of 50, I will have 10x10 2d inventory.

Problem is, I can't seem to do that the right way! Here's what I'm doing:

1- The slot size is set in the bag from the inspector (like 50 for example)
2- When I create the slot at run time, I have to figure out its right scale right?

This is what I'm doing for that (basically I'm following the formula: newScale = (newSize * currentScale) / currentSize):

  1. realPixelScale =  NGUITools.Get3dPixelScale(background.cachedTransform, new Vector2(SlotSize, SlotSize));
  2.  

Where (please don't get lost, it's simple):
  1.         public static Vector3 Get3dPixelScale(Transform trans, Vector3 newSize)
  2.         {
  3.                 var currentSize = GetObjectScreenDims(trans);
  4.                 return Get3dPixelScale(currentSize, new Vector2(newSize.x, newSize.y), trans.localScale);
  5.          }
  6.  
  7.         // Gets an object dimensions in screen coords
  8.         public static Vector3 GetObjectScreenDims(Transform t)
  9.         {
  10.                 var cam = FindCameraForLayer(t.gameObject.layer);
  11.                 var bounds = GetObjectBounds(t);
  12.                 var sMax = cam.WorldToScreenPoint(bounds.max);
  13.                 var sMin = cam.WorldToScreenPoint(bounds.min);
  14.                 return sMax - sMin;
  15.          }
  16.  
  17.         public static Vector3 Get3dPixelScale(Vector3 currentSize, Vector3 newSize, Vector3 currentScale)
  18.         {
  19.                 Vector3 newScale = Get2dPixelScale(currentSize, newSize, currentScale);
  20.                 newScale.z = (newSize.z * currentScale.z) / currentSize.z;
  21.                 newScale.z = Mathf.Clamp(newScale.z, 1, newScale.z);
  22.                 if (float.IsNaN(newScale.z))
  23.                         newScale.z = 1;
  24.                 return newScale;
  25.         }
  26.  
  27.         public static Vector2 Get2dPixelScale(Vector2 currentSize, Vector2 newSize, Vector2 currentScale)
  28.         {
  29.                 var newScale = new Vector3();
  30.                 newScale.x = (newSize.x * currentScale.x) / currentSize.x;
  31.                 newScale.x = Mathf.Clamp(newScale.x, 1, newScale.x);
  32.                 newScale.y = (newSize.y * currentScale.y) / currentSize.y;
  33.                 newScale.y = Mathf.Clamp(newScale.y, 1, newScale.y);
  34.                 return newScale;
  35.         }
  36.  


So now, I 'should' have the right scale for the slot, such that its height and width are 'SlotSize'

Now, in my bag:
  1. var dims = NGUITools.GetObjectScreenDims(background.cachedTransform);
  2. nMinCols  = (int)(dims.x / slotSize);
  3. nMinRows = (int)(dims.y / slotSize);
  4.  

It doesn't work :( - For some reason, I get more slots than I should (the slot is getting a higher scale than it should. ex: I give it a 60 size, I get a scale of (70, 70, 1) :( - I am 100% sure that for some reason, this script used to work just fine with me, but now it's not...

What is the right way to do what I want? what should I calculate, what should I divide?...  please any help would be appreciated - thanks a lot.

and please let me know if I'm doing something bad / the wrong way (bad habit - miss-use)

Note that I do get better results if I just say:
  1. realScale = new Vector3(slotSize, slotSize, 1); // instead of all that long calculations
  2.  
But sometimes this could cause problems in other places :/

33
Hello, I'm using Tween combinations to create animation effects (Like when you click on the popup list) - I want a good way to execute a method after the animation finishes.

I currently I'm doing something like this: (this is a snippet from my custom context menu, which is basically a popup list that appears on the mouse position)

  1. // show on mouse, and seek to animate
  2. if (show && hasFinshedAnimating) {
  3.         _transform.position = camera.ScreenToWorldPoint(Input.mousePosition);
  4.         if (isAnimated) {
  5.                 hasFinshedAnimating = false;
  6.                 for (int i = 0, len = entries.Count; i < len; ++i) {
  7.                         Animate(entries[i]);
  8.                 }
  9.         UIWidget.Animate(background, animSpeed);
  10.         StartCoroutine(WaitForAnim());
  11.         }
  12. }
  13.  
  14. IEnumerator WaitForAnim()
  15. {
  16.         yield return new WaitForSeconds(animSpeed + .15f);
  17.         hasFinshedAnimating = true;
  18.         if (Random.Range(0, 10) > 6)
  19.                 ResetEntriesDims();
  20. }
  21.  
  22. void Animate(Entry entry)
  23. {
  24.         UIWidget.Animate(entry.background, animSpeed);
  25.         UIWidget.Animate(entry.label, animSpeed);
  26. }
  27.  
  28. public static void Animate(UIWidget widget, float animSpeed)
  29. {
  30.         AnimateColor(widget, animSpeed);
  31.         AnimatePosition(widget, animSpeed);
  32.         AnimateScale(widget, animSpeed);
  33. }
  34.  
  35.  

(Notice I'm actually calling Animate methods, but they internally operate on Tween)

It works fine, serves the purpose. But I wonder if there's something built-in for that, something I'm missing perhaps, anything?

In general, can this be done in a better more elegant way?

Thanks!

34
NGUI 3 Support / Zoomable clipped panel? (zoom in/out the contents)
« on: September 13, 2013, 03:22:50 AM »
Hello,

I am looking for a way, if possible to zoom in/out to/of the clipped panel's contents.

Now I have done this before by using an external orthographic camera, changing its rect to the area I want to zoom in to, and then mess with its size to zoom in/out. (See attachment)

Now this is OK if I don't wanna interact with the object I'm zooming in/out to/from. Obviously for this to work, I had to make a separate panel for the object in question (the wooden sprite) - Got this info from UIWidget.cs:

  1.         public void CheckLayer ()
  2.         {
  3.                 if (mPanel != null && mPanel.gameObject.layer != gameObject.layer)
  4.                 {
  5.                         Debug.LogWarning("You can't place widgets on a layer different than the UIPanel that manages them.\n" +
  6.                                 "If you want to move widgets to a different layer, parent them to a new panel instead.", this);
  7.                         gameObject.layer = mPanel.gameObject.layer;
  8.                 }
  9.         }
  10.  

But then of course, when you mess around with layers (because I should remove the object I'm zooming in/out layer from my main gui cam culling mask, so that only my external cam sees the object - the wooden sprite), ray might not hit colliders, I had a lot of trouble getting the collider on the wooden sprite to detect mouse clicks, I rarely, in some very unique cases managed to, I was never able to get it work.

Anyway, that's not my question, I was just showing you my attempt to create a zoom in/out effect. I will post in much more detail about the problems I had there in another post.

But right now, since my way of doing it was very tedious, I thought well, NGUI must have something to achieve this, Aren wouldn't let something like this pass by now would he? ;-)

So, is there an NGUIsh way, an elegant way to achieve zooming in/out?

- Thanks all in advance.

EDIT: I have just noticed that messing around with the panel's scale (that has only the wooden sprite) could do the job for me, but of course, messing with a panel scale is a taboo in NGUI :(  (See other attachment)


35
I have asked the question here << http://answers.unity3d.com/questions/533602/best-waypattern-to-exchange-messages-between-gui-e.html >> I got an answer, but it wasn't that good. I can't send parameters to my functions with UIButtonMessage.

So, what do you think the best way to communicate with UI elements?

- Thanks.

36
Hello, with the current UIInput, you can click on a label and the caret will be at the last char and you can edit.

What I'm looking for to have:
1- Click anywhere within the label and have the caret be at that position.
2- I can select/highlight text.

Any ideas how I can achieve those two? I just wanna know where to start and how am I to go about doing this, especially the highlighting part.

This would also be a really nice feature to have in future NGUI releases.

Thanks.

37
I searched a lot, I found misc problems similar to what I'm having, all circle around "ResetPosition" - it didn't work for me.

What I have is, a UIPanel which has a UIDraggablePanel script on it, it has one child "Table" which is a gameObject that has a UITable attached on it. I add prefabs to the table via AddChild.

I position the panel correctly before playing, after I hit play the panel changes its position (not clipping bounds) and the position of its child (the table). I tried playing without adding anything to the table, same thing. I tried calling ResetPosition numerous times in numerous positions (even in Update), no dice. (See attachment)

Everytime I hit play, it just keeps changing and changing.

Why is this happening and how to make my panel maintain its starting position?

Thanks.

EDIT: I've included my setup as an attachment.

38
Hello, I'm working on a file manager. One of the rules is that you can't have duplicate names. I have written the code to give me a unique name just fine. And I'm using it:

1- each time I create a new folder/file.
2- after I submit the changes to a file/folder's name (inside OnSubmit(string name))

Problem is, you see whenever I create a new folder, I give my input 'selected = true' so that I can name a folder right after I create it, BUT if I deselect the input (click elsewhere), OnSubmit doesn't seem to trigger, instead OnSelect(false) is what's being called, and then I can end up with multiple folders having the same name (due to mDefaultText). (See attachment) So I should also inject my code in there as well, specifically:

  1. if (string.IsNullOrEmpty(mText))
  2. {
  3.         label.text = mDefaultText;
  4.         // TODO: INJECT
  5.         //
  6.          here...
  7.         //
  8.         label.color = mDefaultColor;
  9.         if (isPassword) label.password = false;
  10. }
  11. else label.text = mText;
  12.  

I tried inserting a call to 'onSubmit(label.text);' because I do have my code there - but I got an exception onSubmit is null :(

Can I assign onSubmit somehow? Or do I have to make my own custom inherited UIInput and then override OnSelect?

Thanks :D


And here's my code if somebody wants to benefit :D
(I give it the names of all the contents of the current folder, and the name I'm trying to name a certain file/folder with and it gives me back a unique name for that file/folder, in the fashion of file_N, file_N+1, ... file_N+n. Just like MS Windows)
  1. public static string GetUniqueName(List<string> names, string name)
  2. {
  3.         int index = MiscOps.LinearSearch(names.ToArray(), name);
  4.         if (index == -1)
  5.                 return name;
  6.  
  7.         if (!name.Contains("_"))
  8.                 return GetUniqueName(names, name + "_0");
  9.  
  10.         short n;
  11.         string postfix = name.Substring(name.LastIndexOf('_') + 1);
  12.         bool isNum = Int16.TryParse(postfix, out n);
  13.  
  14.         if (!isNum)
  15.                 return GetUniqueName(names, name + "_0");
  16.  
  17.         int postfixNum = Convert.ToInt16(postfix);
  18.         postfixNum++;
  19.         name = name.Substring(0, name.LastIndexOf('_') + 1);
  20.         name += postfixNum.ToString();
  21.         return GetUniqueName(names, name);
  22. }
  23.  

39
Hello, are we? - Well, I guess yes otherwise code wouldn't be shipped with full NGUI, right?

40
This is really strange, but sometimes I get this behavior where my widgets disappear if I look at them from a certain camera angle in the scene view, and re-appear if I look at them from another angle! - See attachment.

This happens most the times when I have more than one panel, it evens happens to me in some of the example scenes, like the clipping scroll view.

It's really annoying, how to fix it? why is it happening?

Thanks.

41
NGUI 3 Support / How to create a rectangle selection? (selection box)
« on: September 04, 2013, 05:03:51 AM »
Hello again :D

As you probably know, I'm working on a file manager. Some very basic thing that you should be able to do, is to select multiple files/folders with your mouse using a rectangle selection. I have set up Multiple selection just fine (click on file, hold ctrl, click on others to add them to the current selection) - See attachments.

What I thought about is, draw a simple Sprite, let its beginning (start position) be where the mouse clicks first, and then when you move the mouse, the sprite scales along with you. There are of course tricky things that I have to watch out for, like a negative scale which is why I think should change the sprite's pivot point appropriately, like:

  1. #Psudo code
  2.  
  3. var start = mouse.pos;
  4. if (mouse moves right and up 'start')
  5.     sprite.pivot = bottomLeft;
  6. (... some more ifs covering the rest of possibilities ...)
  7. whenever the mouse moves:
  8.     sprite.scale = dist(cursor, sprite);
  9.  

Or something along that - And then, the way I'm thinking to detect collision between my files/folders and the rect selection, is via the colliders - if (there is a collision between the rect and a file/folder => add the colliding file/folder to the selection)
Prob solved.

Now I'm asking because I believe there is a better and maybe easier way to draw the rect, the way I mentioned isn't all that walk in the park.

So, what are you ideas/suggestions in achieving a rectangle selection? in other words having a sprite follow the cursor and scaling nicely with it? in other words draw a rectangle between two points?

- Thanks a bunch for any help/ideas!

42
Hello, I'm working on a file manager where you have files and folders inside a background.
Every folder has contents which are stored in a List<FolderContents> - A content could be a File or Folder. Each content has an index, that way I can position the content like this:

  1. public void PositionContent(FolderContent content, int atIndex)
  2. {
  3.         // since I'm placing the contents in a 1 dimensional list, I have to get the 2 dimensional index of the content to position it
  4.         // nContentsToFit is how many contents fits inside the current folder in terms of rows / cols (ex 4x4) (related to the dimensions of the folder's background)
  5.         // an example of the offset: the 6th content has an index of 5 (in 1d), now in a 4x4 folder its 2d index is (1,1) (2nd row, 2nd col)
  6.         // Index2D is just a struct holding two integers, much like a V2 but for ints instead of floats
  7.         var offset = new Index2D(atIndex / nContentsToFit.col, atIndex % nContentsToFit.j);
  8.        
  9.         // ContentWidth is the width of the folder/file's background -- which is the problem
  10.         float x = offset.col * ContentWidth + offset.col * spaceBetweenContents;
  11.         float y = offset.row * ContentHeight + offset.row * spaceBetweenContents;
  12.         var trans = content.cachedTransform;
  13.         trans.position = topLeft.position;  // an empty gameObject to start positioning the contents from
  14.         var screenpos = guiCamera.WorldToScreenPoint(trans.position);
  15.         screenpos.x += x;
  16.         screenpos.y -= y;
  17.         trans.position = guiCamera.ScreenToWorldPoint(screenpos);
  18. }
  19.  

Now, at first, when everything was working OK, "ContentWidth" and "ContentHeight" were the same as the scale of the background, but then for some reason I had some trouble and moved my panel under another UI, which is when I noticed a very TERRIBLE positioning of files/folders. I knew the reason is the new UI, I tried finding the dif between them, they were the same :(( - I think now the values of the scale are actual screen pixels.

(Please see attachment to see the dif between after and before)

FYI, I have more than one camera and 2 UI.
I'm not developing for any mobile, only PC.

PLEASE for the love of anything you love, just let me understand how NGUI coords work!
0- what happened with me, why did everything got so messed up?
1- what is NGUI virtual pixels?
2- what is the relation between NGUI virtual pixels and screen coord?
3- how does scaling work in NGUI?
4- what does pixel perfect mean?? (please don't tell me "Pixel-perfect means making the pixels perfect" or something similar, cuz I've heard that a lot and it doesn't help clear anything)
5- what is UIRoot.pixelSizeAdjustment?? ("A helper function that figures out the pixel size adjustment for the specified object" - similar explanation to Pixel-perfect, pixel-perfect means the pixels are perfect, aughhhh)
6- when is it that the scaling values are the same as in onscreen pixels?
7- what is the relation between the scale values of an object and its width/height?

Sorry I know it's a lot but please I want to understand how all those things are tied together.... PLZZZZZ ME NEEDZ EXZBLAIN! <Shrek cat look> :(

I Have looked and searched a lot in the forums, the more I do the more I get confused :(( - a lot of what I got are half-missing the info I want.

Let this be a full reference to this subject for those who have the same 'suffering', because there are a lot...

Thank you SO MUCH in advance! "ANYTHING" - would be VERY MUCH appreciated!

And PLZZZZZZZZZZZ Aren, make us a better documentation *___*


EDIT: Problem solved using localPosition in the right place. I've also found out about UITable which does the organizing for me automatically, like UIGrid but for 2D context instead :)


43
Hello, I have a script called TextFile, that I only want to enable once I pickup the text file game object, it has a lot of stuff that I must set AFTER I pick it up. Some of those stuff are accessed in events such as OnHover, so if I hover over the gameobject with the TextFile BEFORE picking it up I get en exception, things are not set yet. So all I should do, is disable the script at first, and when I pick it up (which is done from another FilePickupScript attached to the game object as well) I enable the TextFile and assign the missing stuff.

But the problem is, even though I disabled the TextFile script, its events still got triggered! (OnHover got called!)

What is going on? why is this happening?

44
NGUI 3 Support / [SOLVED] Double click problem, not fast enough.
« on: September 03, 2013, 08:02:15 AM »
Hello, I'm working on a file manager where you have files and folders and basic operations you could do on them (See attachment)

You can open a file/folder by double clicking on it, for that I'm using OnDoubleClick() event. Problem is that it's not fast enough i.e. if I click on a folder 'a', and then quickly click over another folder 'b' I open up 'b' instead! - This shouldn't happen right?

How can I get around this / fix it? - Thanks.

45
NGUI 3 Support / [SOLVED] widget color alpha not changing?
« on: September 03, 2013, 01:05:38 AM »
Hello, this is pretty noob of me.  I want to access the 'tint color' of a widget, which is from my understanding is the 'color' property. I'm trying to change the alpha of my widget, (just like you do when you mess with the 'A" slider when you click on tint color).

I tried both myWidget.alpha = myFloatValue; and myWidget.color.a = myFloatValue; - The first had no effect, the second didn't compile.

Do I have to make a new color, copy the rgb values from the old color and apply the new alpha? - Isn't this how the alpha property is being set anyway? - Thanks.

Pages: 1 2 [3] 4