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 - junkier

Pages: [1]
1
Hi!

I tried to search this question but no results...

So, I can't find checkbox in UIWidget's inspector window, that will auto-resize attached BoxCollider2D automatically.

And as I understand, I'm not allowed to mix BoxColliders & BoxCollider2Ds under one UICamera on one Layer (for properly touches handling),
so I need to use only BoxCollider2D if I switch UICamera to Unity2D Event Type.

So is 'auto-adjust' for BoxCollider2D feature scheduled for furher updates? Or I need to use some workarounds or hacks?
Thank you!


Edit:
As for now, I found 'hack' solution:
1) Add UIWidget and BoxCollider components to some GameObject;
2) In UIWidget's inspector window will apear 'auto-adjust to match' checkbox. Set it to Checked.
3) Remove BoxCollider and then add BoxCollider2D.
4) Then add some code to UIWidget.cs:

  1. public void ResizeCollider ()
  2. {
  3.     if (NGUITools.GetActive(this))
  4.     {
  5.         BoxCollider box = collider as BoxCollider;
  6.         if (box != null) NGUITools.UpdateWidgetCollider(box, true);
  7.        
  8.                 // ** Modified for BoxCollider2D
  9.                 else
  10.                 {
  11.                     var box2D = collider2D as BoxCollider2D;
  12.             if (box2D == null)
  13.                 return;
  14.             Vector3[] corners = localCorners;
  15.             box2D.center = Vector3.Lerp(corners[0], corners[2], 0.5f);
  16.             box2D.size = corners[2] - corners[0];
  17.                 }
  18.         // **
  19.     }
  20. }

5) Now our BoxCollider2D appears to be updated when UIWidget's dimensions change.

2
This is already supported by NGUI natively. Change the event type on the UICamera.

Wow... I'm confused... It seems I've missed it in versions notes. Thanks!

3
I wanted to use NGUI's touch messaging, so I had to do this. it wasn't bad.
all of the changes are in UICamera. Change all of the Physics to Physics2D and the RaycastHit to RaycastHit2D.

finally the 2d raycast cannot provide an "out" ray as an argument like the 3d raycast, and it does not return a bool, so there is a slight logic change...

what used to look like:
if ((Physics.Raycast (ray, out hit, dist, mask))

now looks like:
hit = Physics2D.Raycast (ray.origin, ray.direction, dist, mask);
if (hit.collider != null)

(don't forget you have to change all of the RaycastHit to RaycastHit2D and Physics to Physics2D, so that hit is actually a RaycastHit2D).

there is also a similar change with the whole:
RaycastHit[] hits = Physics.RaycastAll(...

becoming:
RaycastHit2D[] hits = Physics2D.RaycastAll(ray.origin, ray.direction, ...

these changes basically make NGUI only detect hits with 2d colliders and not with 3d colliders. I plan on making my UI widgets, then manually changing the rigidbody to rigidbody2d and the collider to collider2d on all of the panels and widgets.


loopyllama, thank you! I will try to add your changes and do not remove 3D colliders support.
I think it would be great to make support for both 2D and 3D types of Colliders in NGUI!

4
NGUI 3 Support / Re: What is BetterList?
« on: March 28, 2014, 06:24:40 AM »
Every time you add an item to a regular List, it re-allocates a new array. This is terrible for performance and garbage collection. Same thing happens when you remove items.
Unity hasn't updated their version of mono in many years, so yes, it's still true.

Hi there!
I was very surprised when I have seen BetterList class. And I was wondering, why System.Collections.Generic.List is so bad, and came there to find answer.
I've performed simple test in Unity Editor (Unity 4.3.4, Unity 3.5 .net full Base Class Libraries), that has shown, as I expected, that System.Collections.Generic.List works well, as expected, with memory.
Please see my example code below:

using System.Collections.Generic;

var list = new List<int>();
Debug.Log("Initial capacity: " + list.Capacity);
for (int i = 0; i < 10; ++i)
{
  list.Add(i);
  Debug.Log(string.Format("Added element '{0}'. Current capacity = {1}", i, list.Capacity));
}

list.Clear();
Debug.Log("List capacity after clear: " + list.Capacity);

// Filling list for second time
for (int i = 0; i < 10; ++i)
{
  list.Add(i);
  Debug.Log(string.Format("Added element '{0}'. Current capacity = {1}", i, list.Capacity));
}

list.Clear();
Debug.Log("List capacity after clear: " + list.Capacity);

And the result:

Quote
Initial capacity: 0
Added element '0'. Current capacity = 4
Added element '1'. Current capacity = 4
Added element '2'. Current capacity = 4
Added element '3'. Current capacity = 4
Added element '4'. Current capacity = 8
Added element '5'. Current capacity = 8
Added element '6'. Current capacity = 8
Added element '7'. Current capacity = 8
Added element '8'. Current capacity = 16
Added element '9'. Current capacity = 16
List capacity after clear: 16
Added element '0'. Current capacity = 16
Added element '1'. Current capacity = 16
Added element '2'. Current capacity = 16
Added element '3'. Current capacity = 16
Added element '4'. Current capacity = 16
Added element '5'. Current capacity = 16
Added element '6'. Current capacity = 16
Added element '7'. Current capacity = 16
Added element '8'. Current capacity = 16
Added element '9'. Current capacity = 16
List capacity after clear: 16

So, as we can see, new array allocated only when we trying to add more elements than capacity allows. If you know beforehand how much elements you are going to have in a list, you can pass this number to List's constructor, and it will allocate in memory array with corresponsind size.
All what I'm trying to say, that now System.Collections.Generic.List is working in Unity just like MSDN says (at least for .NET 3.5).


Pages: [1]