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). 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 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 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. 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: 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. 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. Takes a object reference. It may be used if your whole scene is a prefab. // 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. There are some things that you need to set here: 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. 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: 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. 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. Takes a object reference. It may be used if your whole scene is a prefab. // bar.ValueF = 1.0f; - will fill the bar to maximum value ~ CG 數位學習網版權所有 ~ |