Energy Bar Toolkit starting with version 1.5.1 is designed to be used within C#, JavaScript, or even Boo projects. The problem with using multiple languages in a single project is how Unity compiles script files . The problem can be solved by moving script files to Standard Assets directory but this is not acceptable solution for everyone. Here you can read about known mehtods of using Energy Bar Toolkit in JavaScript or Boo projects.

The “Standard Assets” approach

This is the simplest one and in the most cases should work OK. The only thing that you need to do is to rearrange directory structure of Energy Bar Toolkit asset. What you need to do is:

  1. Create Standard Assets directory in your project root (Assets)
  2. Move Energy Bar Toolkit / Scripts / Editor directory to Energy Bar Toolkit / Editor
  3. Move Energy Bar Toolkit / Scripts / 3DRenderers / Editor directory to Energy Bar Toolkit / Editor
  4. Move Energy Bar Toolkit / Scripts directory to Standard Assets / Energy Bar Toolkit
You can create any subdirectory structure that you want. The main rules are that all scripts except editor script must be placed in Standard Assets root directory.

Accessing using Event System

You can access energy bars without any programming at all. All you need to know is to how EBT Event System works.

Accessing using SendMessage()

Energy bars can be accessed by SendMessage function. In this way you can manipulate the most basic settings of energy bar. Here are the functions that can receive messages:

  • SetValueCurrent(int) – sets the current value of energy bar
  • SetValueMin(int) – sets the lower value boundary
  • SetValueMax(int) – sets the higher value boundary
  • SetValueF(float) – sets the current value as percent (0.0 – 1.0)

C# Example

public GameObject energyBar;

public void Change() {
    energyBar.SendMessage("SetValueF", 0.5f);
}

JavaScript Example

var energyBar : GameObject;

function Change() {
    energyBar.SendMessage("SetValueF", 0.5);
}

Boo Example

energyBar as GameObject

def Change():
    energyBar.SendMessage('SetValueF', 0.5F)

Accessing using accessor classes

The worst scenario is when you need more advanced access to EnergyBar classes during runtime.

If you really need to change bar properties during runtime from JavaScript or Boo then consider writing some kind of C# accessor. The accessor should directly access EnergyBar methods and fields and it should listen for messages sent by SendMessage() function.

Here’s a little example:

EnergyBarAccessor.cs

This component should be attached to the same game object that energy bar renderer is. It gets the energy bar script and gives SetAnchorObject() method that can be called by SendMessage():

public class EnergyBarAccessor : MonoBehaviour {
    
    EnergyBarBase energyBarBase;

    void Start() {
        energyBarBase = GetComponent<EnergyBarBase>();
    }

    void SetAnchorObject(GameObject anchorObject) {
        energyBarBase.anchorObject = anchorObject;
    }
}

AccessingInJS.js

This component is an example of how accessor should be called. You should attach gameobject with energy bar and anchor object in inspector to make it work:

var energyBar : GameObject;
var anchorObject : GameObject;

function OnGUI() {
    if (GUI.Button(Rect(10, 10, 100, 20), "Set Up Anchor")) {
        energyBar.SendMessage("SetAnchorObject", anchorObject);
    }
}

You can download package with these files and an example of scene to see it working is here