Author Topic: Using Tenkoku with C# (Reflection example)  (Read 1824 times)

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Using Tenkoku with C# (Reflection example)
« on: January 29, 2016, 05:31:07 PM »
Edit: a C# version of Tenkoku version 1.1.0 is now available.  The below information still applies to the .JS version.

In response to a number of requests about using Tenkoku with C# I've decided to write the below mini-tutorial about using Reflection.  While the current release of Tenkoku is indeed in .JS, this does not mean you can't read and access Tenkoku variables in your C# script.  While there are a few different ways you can do this, the easiest is to use Reflection in order to read Tenkoku components and then pass variable adjustments.  This can be done directly in your C# components with only a couple of extra added lines.

For example, let's assume the simple case of wanting to set the current hour to 10am.  You would first tell your C# component that you want to use reflection by including the "using System.Reflection" line at the top of your component.  This allows access to Reflection commands...
Code: [Select]
using System.Reflection;
Next you would find and cache the Tenkoku components so you can access them later (just as you would in regular C#)...
Code: [Select]
GameObject tenkokuObj = GameObject.Find("Tenkoku DynamicSky");
if (tenkokuObj == null){
      Debug.LogWarning("Unable to find Tenkoku object!");
      return;
} else {
     var tenkokuModule = tenkokuObj.GetComponent("TenkokuModule");
}

Assuming you do have Tenkoku in your scene, all you need to do to set the time is to search for the correct variable (in this case "currentHour") and then send the correct value back to the Tenkoku component...
Code: [Select]
if (tenkokuModule != null){
      FieldInfo tHour = tenkokuModule.GetType().GetField("currentHour", BindingFlags.Public | BindingFlags.Instance);
       if (tHour != null) tHour.SetValue(tenkokuModule, setHour);
}


That is all it takes to get reflection working.  It's only a couple lines longer than the normal C# code would have taken.  Below (and attached) is an example of a full script of the above, that exposes the hour variable so it can be publicly changed in the Unity Editor.  Feel free to copy this component to your project and experiment on your own.

Code: [Select]
using UnityEngine;
using System;
using System.Reflection;

public class Test_Reflection : MonoBehaviour {

    public int setHour = 8;

    void LateUpdate(){

        //Check scene for Tenkoku Module and cache it for future use
        GameObject tenkokuObj = GameObject.Find("Tenkoku DynamicSky");
        if (tenkokuObj == null){
            Debug.LogWarning("Unable to find Tenkoku object!");
            return;
        } else {

            //Edit Tenkoku Settings
            var tenkokuModule = tenkokuObj.GetComponent("TenkokuModule");
            if (tenkokuModule != null){

                //Set the hour
                FieldInfo tHour = tenkokuModule.GetType().GetField("currentHour", BindingFlags.Public | BindingFlags.Instance);
                if (tHour != null) tHour.SetValue(tenkokuModule, setHour);

            }
        }
    }
}
« Last Edit: May 01, 2016, 09:11:56 AM by Chingwa »