Author Topic: how to subclass uipanel?  (Read 6345 times)

cyx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 17
    • View Profile
how to subclass uipanel?
« on: March 09, 2014, 12:11:28 PM »
sometimes i'm feeling pretty stupid... i managed it to subclass uilabel with the follwing code:

  1. [ExecuteInEditMode]
  2. [AddComponentMenu("NGUI/UI/NGUI Label")]
  3. public class MyUILabel : UILabel
  4. {
  5. //
  6. }
  7.  

Perhaps this is the wrong way because it's not working the same with UIPanel. How can I subclass UIPanel without loosing the possibility to work with your property checkboxes.

Thanks in advance.

cyx
« Last Edit: March 09, 2014, 06:12:57 PM by cyx »

cyx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: how to subclass uipanel?
« Reply #1 on: March 10, 2014, 10:30:51 AM »
I still need support to understand how to create a subclass of UIPanel, I know I can add another component and copy the script, but how would it work to subclass it and see all the properties in the unity inspector? Thanks guys

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how to subclass uipanel?
« Reply #2 on: March 10, 2014, 07:43:32 PM »
Why are you trying to do this? A panel is what collects and draws widgets. It's not meant to be derived from.

And neither is the UILabel, actually. The label is a widget that prints text. Why are you deriving from it? What are you trying to change that you can't do in a stand-alone script?

cyx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: how to subclass uipanel?
« Reply #3 on: March 13, 2014, 09:14:47 AM »
I just wanna add the possibility to set and get x and y coordinates as a property like panel.x = 100 instead of writing .transform.localPosition = new Vector.... because I'm moving alot stuff around and I try to make my code as short as possible. So what would you do to add this behaviour? Thanks Aren.

N3uRo

  • Guest
Re: how to subclass uipanel?
« Reply #4 on: March 13, 2014, 02:43:25 PM »
I just wanna add the possibility to set and get x and y coordinates as a property like panel.x = 100 instead of writing .transform.localPosition = new Vector.... because I'm moving alot stuff around and I try to make my code as short as possible. So what would you do to add this behaviour? Thanks Aren.

Extension methods...

http://msdn.microsoft.com/en-us/library/bb383977.aspx

In our team we have a method for each Axis for position, rotation and scale (world and local), it keeps our code cleaner and smaller.

Here you have:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public static class TransformExtensions {
  5.  
  6.     public static void SetPositionX(this Transform transform, float x) {
  7.         transform.position = new Vector3(x, transform.position.y, transform.position.z);
  8.     }
  9.  
  10.     public static void SetPositionY(this Transform transform, float y) {
  11.         transform.position = new Vector3(transform.position.x, y, transform.position.z);
  12.     }
  13.  
  14.     public static void SetPositionZ(this Transform transform, float z) {
  15.         transform.position = new Vector3(transform.position.x, transform.position.y, z);
  16.     }
  17.  
  18.     public static void SetLocalPositionX(this Transform transform, float x) {
  19.         transform.localPosition = new Vector3(x, transform.localPosition.y, transform.localPosition.z);
  20.     }
  21.  
  22.     public static void SetLocalPositionY(this Transform transform, float y) {
  23.         transform.localPosition = new Vector3(transform.localPosition.x, y, transform.localPosition.z);
  24.     }
  25.  
  26.     public static void SetLocalPositionZ(this Transform transform, float z) {
  27.         transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, z);
  28.     }
  29.  
  30.     public static void SetRotationX(this Transform transform, float x) {
  31.         transform.rotation = Quaternion.Euler(new Vector3(x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));
  32.     }
  33.  
  34.     public static void SetRotationY(this Transform transform, float y) {
  35.         transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, y, transform.rotation.eulerAngles.z));
  36.     }
  37.  
  38.     public static void SetRotationZ(this Transform transform, float z) {
  39.         transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, z));
  40.     }
  41.  
  42.     public static void SetLocalRotationX(this Transform transform, float x) {
  43.         transform.localRotation = Quaternion.Euler(new Vector3(x, transform.localRotation.eulerAngles.y, transform.localRotation.eulerAngles.z));
  44.     }
  45.  
  46.     public static void SetLocalRotationY(this Transform transform, float y) {
  47.         transform.localRotation = Quaternion.Euler(new Vector3(transform.localRotation.eulerAngles.x, y, transform.localRotation.eulerAngles.z));
  48.     }
  49.  
  50.     public static void SetLocalRotationZ(this Transform transform, float z) {
  51.         transform.localRotation = Quaternion.Euler(new Vector3(transform.localRotation.eulerAngles.x, transform.localRotation.eulerAngles.y, z));
  52.     }
  53.  
  54.     public static void SetLocalScaleX(this Transform transform, float x) {
  55.         transform.localScale = new Vector3(x, transform.localScale.y, transform.localScale.z);
  56.     }
  57.  
  58.     public static void SetLocalScaleY(this Transform transform, float y) {
  59.         transform.localScale = new Vector3(transform.localScale.x, y, transform.localScale.z);
  60.     }
  61.  
  62.     public static void SetLocalScaleZ(this Transform transform, float z) {
  63.         transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, z);
  64.     }
  65. }
  66.  

Then you can do:

  1. transform.SetLocalPositionX(5.5f);
  2.