Energy BarTool kit 教學.CG數位學習網

CG 數位學習網 > EnergyBarToolkit 教學

跟隨 3D 遊戲物件

使用 Energy Bar Toolkit bars 製作的血條,提供自動跟隨遊戲物件的功能,若需要顯示遊戲角色的血量(例如即時戰略遊戲)時,是一個很實用的功能。

General approach

為了讓血條顯示於物件的上方或下方,建議在需要跟隨的遊戲物件建立一個空物件當做子物件。

In this way you have a full control where in 3D space your bar should be displayed.

使用 UGUI 的方法

This will work only if you’re using uGUI renderers.

Anchoring with Unity UI is very simple. Just follow these steps:

Select the bar object that you want to be a follower

執行 [ Tools > Energy Bar Toolkit > uGUI > Add Components > Follow Object ] 若選取的物件類型不是★,將會出現錯誤訊息。★

In your bar inspector edit Energy Bar Follow Object component and:

Set the Follow Object field to the object or anchor that you want to follow

Set the camera that is used to render that object (通常是 Main Camera)

Following 3D World Object

Energy Bar Toolkit bars have the ability to follow any 3D object on your scene. It is very useful for games where you have characters with health status that should be displayed right above or beyound them (like for strategy games).
General approach

To make your bar be displayed below or above it’s recommended to create an empty game object child under the 3D model object that you want to follow.

In this way you have a full control where in 3D space your bar should be displayed.

使用 uGUI 的方法

This will work only if you’re using uGUI renderers.

Anchoring with Unity UI is very simple. Just follow these steps:

Select the bar object that you want to be a follower

From the main menu select Tools → Energy Bar Toolkit → uGUI → Add Components → Follow Object

In your bar inspector edit Energy Bar Follow Object component and:

Set the Follow Object field to the object or anchor that you want to follow

Set the camera that is used to render that object (usually it will be the Main Camera)

透過 Optionally set the Offset 微調位置

You can find the working example in uGUI Follow Objects scene in the package.

Spawning objects with bars attached

The common use case is to spawn objects during gameplay that should have the bar attached. There’s a little issue with this approach. You have to make sure to:

Instantiate your 3D object

Instantiate your bar under Canvas object

Change Follow Object settings to follow the newly created object

Save this association somewhere, so when you will destroy the 3D object, the bar will be destroyed too
Normally this would require programming skills, but Energy Bar Toolkit from version 3.0.3 introduces new Spawner component that will help you to setup this kind of scenario without pain!
It’s really simple. Just follow these steps:

Create a new bar prefab that should follow your 3D object

Select your 3D object (prefab) that you want your bar to follow

執行 [ Tools > Energy Bar Toolkit > uGUI > Add Components > Spawner ]

There are some things that you need to set here:

Bar Prefab – The bar prefab created in step one

Attach Point – This should be the current object or if you created a special empty game object that your bar should be attached to (it should be under the current object in the hierarchy) then set it here

Canvas – Canvas lookup settings. Read below how lookups are working.

World Camera – World camera lookup settings. This should be set to the camera that will render this 3D object. Read below how lookups are working.

Network Instantiate – Enable this if this object should use Network.Instantiate instead of Object.Instantiate

Network Group – Network Group when using Network Instantiate
Attaching this script will make sure that:
On instantiate the bar will be instantiated under given Canvas object and will use World Camera to compute the 3D object screen position.
On destroy the bar will be destroyed along with 3D object.
You will find the example of spawner component in example scene at Energy Bar Toolkit / Example / Example 2 – Instantiate.
Lookup methods

Lookup methods is a way to find an object on the scene. If you’re instantiating a prefab then making a direct reference may not always work, so using a lookup is a most efficient way to find your object.
Lookup method – By Path

This method takes a Path as an argument. Use it if you know that your object will be always called the same and set at the same position. Examples:
/Canvas – Canvas is always created on top of hierarchy
/Main Camera – Standard name and position for the default camera
Pros:
You don’t need to set anything more
Cons:
If your object name or hierarchy path changes, the lookup will fail
Lookup method – By Tag

This method takes a Tag as an argument. It will use first object with used tag into the account. It’s really good for MainCamera because the main Unity3D camera must have this tag to be set.
Pros:
Your object may be anywhere in the hierarchy with any name
Cons:
You have to remember to set the tag to only one object per scene
Canvas does not have the tag set at default
Lookup method – By Type

It just searches for the first object of desired type (you don’t need to set anything). If you have only one canvas and one camera, this is the simplest and safest method.
Pros:
The simplest and safest until you have only one object per scene of given type
Cons:
The slowest of all lookup methods.
Lookup method – By Reference

Takes a object reference. It may be used if your whole scene is a prefab.
Pros:
Standard Unity3D way to reference another object
Cons:
When instantiating a prefab the reference can be lost in many scenarios
Accessing spawned bar instance
When your new bar is spawned along with the object, you will need to access its instance. To do that you have to access EnergyBarSpawnerUGUI component from your code. If your script is attached to the same game object as EnergyBarSpawnerUGUI component, the code may look like following:
var spawner = GetComponent<EnergyBarSpawnerUGUI>();
var barRenderer = spawner.instance; // bar renderer allows to access rendering properties
var bar = barRenderer.GetComponent<EnergyBar>(); // allows to change bar value

// bar.ValueF = 1.0f; - will fill the bar to maximum value Offset

You can find the working example in uGUI Follow Objects scene in the package.
Spawning objects with bars attached
The common use case is to spawn objects during gameplay that should have the bar attached. There’s a little issue with this approach. You have to make sure to:
Instantiate your 3D object
Instantiate your bar under Canvas object
Change Follow Object settings to follow the newly created object
Save this association somewhere, so when you will destroy the 3D object, the bar will be destroyed too
Normally this would require programming skills, but Energy Bar Toolkit from version 3.0.3 introduces new Spawner component that will help you to setup this kind of scenario without pain!
It’s really simple. Just follow these steps:
Create a new bar prefab that should follow your 3D object
Select your 3D object (prefab) that you want your bar to follow
From the main menu choose Tools → Energy Bar Toolkit → uGUI → Add Components → Spawner

There are some things that you need to set here:
Bar Prefab – The bar prefab created in step one
Attach Point – This should be the current object or if you created a special empty game object that your bar should be attached to (it should be under the current object in the hierarchy) then set it here
Canvas – Canvas lookup settings. Read below how lookups are working.
World Camera – World camera lookup settings. This should be set to the camera that will render this 3D object. Read below how lookups are working.
Network Instantiate – Enable this if this object should use Network.Instantiate instead of Object.Instantiate
Network Group – Network Group when using Network Instantiate
Attaching this script will make sure that:
On instantiate the bar will be instantiated under given Canvas object and will use World Camera to compute the 3D object screen position.
On destroy the bar will be destroyed along with 3D object.
You will find the example of spawner component in example scene at Energy Bar Toolkit / Example / Example 2 – Instantiate.
Lookup methods

Lookup methods is a way to find an object on the scene. If you’re instantiating a prefab then making a direct reference may not always work, so using a lookup is a most efficient way to find your object.
Lookup method – By Path

This method takes a Path as an argument. Use it if you know that your object will be always called the same and set at the same position. Examples:
/Canvas – Canvas is always created on top of hierarchy
/Main Camera – Standard name and position for the default camera
Pros:
You don’t need to set anything more
Cons:
If your object name or hierarchy path changes, the lookup will fail
Lookup method – By Tag

This method takes a Tag as an argument. It will use first object with used tag into the account. It’s really good for MainCamera because the main Unity3D camera must have this tag to be set.
Pros:
Your object may be anywhere in the hierarchy with any name
Cons:
You have to remember to set the tag to only one object per scene
Canvas does not have the tag set at default
Lookup method – By Type

It just searches for the first object of desired type (you don’t need to set anything). If you have only one canvas and one camera, this is the simplest and safest method.
Pros:
The simplest and safest until you have only one object per scene of given type
Cons:
The slowest of all lookup methods.
Lookup method – By Reference

Takes a object reference. It may be used if your whole scene is a prefab.
Pros:
Standard Unity3D way to reference another object
Cons:
When instantiating a prefab the reference can be lost in many scenarios
Accessing spawned bar instance
When your new bar is spawned along with the object, you will need to access its instance. To do that you have to access EnergyBarSpawnerUGUI component from your code. If your script is attached to the same game object as EnergyBarSpawnerUGUI component, the code may look like following:
var spawner = GetComponent<EnergyBarSpawnerUGUI>();
var barRenderer = spawner.instance; // bar renderer allows to access rendering properties
var bar = barRenderer.GetComponent<EnergyBar>(); // allows to change bar value

// bar.ValueF = 1.0f; - will fill the bar to maximum value

~ CG 數位學習網版權所有 ~