playMaker

Author Topic: NGUI HUD Text Actions  (Read 9228 times)

Flying Robot

  • Sr. Member
  • ****
  • Posts: 293
  • Od ton yebo redro
    • Flying Robot Studios
NGUI HUD Text Actions
« on: October 18, 2013, 01:57:12 AM »
If you have bought NGUI HUD Text. A nice way for attaching NGUI widgets (health bar, dialogs etc) to your NPCs.

https://www.assetstore.unity3d.com/#/content/3831

You will need this to attach the GUI to the target object.

PS. I don't know if Jean is already working on this. Or I'll post the other NGUI HUD actions. Let me know.


AddNGUIFloatingText
« Last Edit: October 21, 2013, 05:53:05 AM by Flying Robot »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Set NGUI Follow Target (For NGUI HUD text)
« Reply #1 on: October 21, 2013, 02:13:32 AM »
Hi,

 by all mean, you are more than welcome to participate!

 Also, pm me with your email if you want to be part of the trello board.

https://trello.com/b/1VSKPLcT/addons

bye,

 Jean

Flying Robot

  • Sr. Member
  • ****
  • Posts: 293
  • Od ton yebo redro
    • Flying Robot Studios
Re: Set NGUI Follow Target (For NGUI HUD text)
« Reply #2 on: October 21, 2013, 05:51:22 AM »
Here's the remaining action of NGUI HUD Text. Thus completing this mini addon :)

I'm maintaining these files at github as they could require updates.

SetNGUIFollowTarget

AddNGUIFloatingText


PS : Sent you PM

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #3 on: February 06, 2017, 08:24:28 PM »
I'm trying to get this to work but I'm having a issue where the character is getting moved to the position of the UI Root (2D) and is invisible.

I'm getting this error.
Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
UIFollowTarget.Update () (at Assets/HUD Text/Scripts/UIFollowTarget.cs:104)
« Last Edit: February 06, 2017, 08:27:01 PM by gearedgeek »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: NGUI HUD Text Actions
« Reply #4 on: February 07, 2017, 12:54:44 AM »
Hi,

 can you paste that script, I am looking at my version here and line 104 is not a line of code, so I am not sure which version of this script you have.

 Bye,

 Jean

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #5 on: February 07, 2017, 12:58:35 AM »
Code: [Select]
// If visible, update the position
if (vis)
{
pos = uiCamera.ViewportToWorldPoint(pos);
pos = mTrans.parent.InverseTransformPoint(pos);
//pos.x = Mathf.RoundToInt(pos.x);
//pos.y = Mathf.RoundToInt(pos.y);
pos.z = 0f;
mTrans.localPosition = pos;
            }

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: NGUI HUD Text Actions
« Reply #6 on: February 07, 2017, 12:59:59 AM »
Hi,

 the whole script :) i need to see what is line 104 in your case. Thanks :)

 bye,

 Jean

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #7 on: February 07, 2017, 01:01:38 AM »
Code: [Select]
//--------------------------------------------
//            NGUI: HUD Text
// Copyright © 2012 Tasharen Entertainment
//--------------------------------------------

using UnityEngine;

/// <summary>
/// Attaching this script to an object will make it visibly follow another object, even if the two are using different cameras to draw them.
/// </summary>

public class UIFollowTarget : MonoBehaviour
{
public delegate void OnVisibilityChange (bool isVisible);

/// <summary>
/// Callback triggered every time the object becomes visible or invisible.
/// </summary>

public OnVisibilityChange onChange;

/// <summary>
/// 3D target that this object will be positioned above.
/// </summary>

public Transform target;

/// <summary>
/// Game camera to use.
/// </summary>

public Camera gameCamera;

/// <summary>
/// UI camera to use.
/// </summary>

public Camera uiCamera;

/// <summary>
/// Whether the children will be disabled when this object is no longer visible.
/// </summary>

public bool disableIfInvisible = true;

/// <summary>
/// Destroy the game object when target disappears.
/// </summary>

public bool destroyWithTarget = true;

Transform mTrans;
int mIsVisible = -1;

/// <summary>
/// Whether the target is currently visible or not.
/// </summary>

public bool isVisible { get { return mIsVisible == 1; } }

/// <summary>
/// Cache the transform;
/// </summary>

void Awake () { mTrans = transform; }

/// <summary>
/// Find both the UI camera and the game camera so they can be used for the position calculations
/// </summary>

void Start()
{
if (target)
{
if (gameCamera == null) gameCamera = NGUITools.FindCameraForLayer(target.gameObject.layer);
if (uiCamera == null) uiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
Update();
}
else
{
if (destroyWithTarget) Destroy(gameObject);
else enabled = false;
}
}

/// <summary>
/// Update the position of the HUD object every frame such that is position correctly over top of its real world object.
/// </summary>

void Update ()
{
if (target && uiCamera != null)
{
Vector3 pos = gameCamera.WorldToViewportPoint(target.position);

            // Determine the visibility and the target alpha
            int isVisible = (gameCamera.orthographic || pos.z > 0f) && (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f) ? 1 : 0;
bool vis = (isVisible == 1);

// If visible, update the position
if (vis)
{
pos = uiCamera.ViewportToWorldPoint(pos);
pos = mTrans.parent.InverseTransformPoint(pos);
//pos.x = Mathf.RoundToInt(pos.x);
//pos.y = Mathf.RoundToInt(pos.y);
pos.z = 0f;
mTrans.localPosition = pos;
            }

// Update the visibility flag
if (mIsVisible != isVisible)
{
mIsVisible = isVisible;

if (disableIfInvisible)
{
for (int i = 0, imax = mTrans.childCount; i < imax; ++i)
NGUITools.SetActive(mTrans.GetChild(i).gameObject, vis);
}

// Inform the listener
if (onChange != null) onChange(vis);
}
}
else Destroy(gameObject);
}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: NGUI HUD Text Actions
« Reply #8 on: February 07, 2017, 01:04:01 AM »
Hi,

 ok, so your GameObject needs a parent. currently it doesn't right?

 Bye,

 Jean

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #9 on: February 07, 2017, 01:12:40 AM »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: NGUI HUD Text Actions
« Reply #10 on: February 07, 2017, 01:17:13 AM »
Hi,

 this doesn't show your hierarchy, basically you should have this on a ngui GameObject, which means it must be parented somehow.

 Bye,

 Jean

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #11 on: February 07, 2017, 01:28:46 AM »
I see. So what I just did was add the "Control - Simple Progress Bar" to my scene. It automatically added the UI Root. Then in playmaker I added the FSM like in my picture and assigned the components to the progress bar. Now in game the progress bar shows up and follows the AI around.

But I noticed that when you turn the player around the progress bar stays at the edge of the screen until you look back at the AI and then it follows the AI again.

Another question, that just came up. In the UI Follow Target, how can you define multiple targets?

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #12 on: February 07, 2017, 01:31:22 AM »
Also how do you update the progress bar of the health?

gearedgeek

  • Playmaker Newbie
  • *
  • Posts: 30
Re: NGUI HUD Text Actions
« Reply #13 on: February 07, 2017, 02:23:18 AM »
Okay, I have figured most of what I asked out.

Not sure how to get multiple progress bars for multiple characters.

How can I turn a digit number (like 68) into a decimal (0.68)? I'm trying to change a float from like 68 to 0.68.
« Last Edit: February 07, 2017, 02:56:36 AM by gearedgeek »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: NGUI HUD Text Actions
« Reply #14 on: February 07, 2017, 04:12:21 AM »
Hi,

 you should have prefabs for your players and HUD, which then means either your player instance manage its own HUD or you have a manager that create both the player and hud instance and tight them together.

 divide 68 /100 and you'll get 0.68, else if 68 comes as a string, use String format action

 Bye,

 Jean