What I would like to do is create a 2D texture and apply it to a Texture which I then draw it out with GUI.DrawTexture. I have done a little searching around and it doesn't look like it is "natively" possible to do such a thing, though I am sure there is probably some way or another to accomplish it.
I have seen some hacks that talk about creating multiple textures and then drawing out one of them according to the direction you are facing, but that seems extremely sloppy to me.
At the moment here is my Compass code (Compass.js):
class Compass extends MonoBehaviour implements CEventListener{
public var myNorth:int;
public var needle:Texture;
private var eventsLoaded:boolean = false;
public function Compass(gpsNorth:int, tex:Texture){
myNorth = gpsNorth;
needle = tex;
}
public function OnGUI():void{
if(!eventsLoaded){
EventManager.getInstance().AddListener(this as CEventListener, "CompassUpdateEvent");
eventsLoaded = true;
}
GUI.DrawTexture(Rect(Screen.width - 100, 20, 82, 82), needle, ScaleMode.StretchToFill, true, 0.0);
}
public function HandleEvent(evt:CEvent):boolean{
if(evt.GetName() == "CompassUpdateEvent"){
var t:Vector3 = evt.GetData()["facingDirection"];
transform.rotation.eulerAngles.y = (myNorth + t.y) % 360;
}
return true;
}
}
class CompassUpdateEvent implements CEvent{
private var obj:System.Collections.Hashtable;
public function CompassUpdateEvent(){
obj = new System.Collections.Hashtable();
}
public function GetName():String{
return "CompassUpdateEvent";
}
public function GetData():System.Collections.Hashtable{
return obj;
}
public function SetData(key:String, value):void{
obj.Add(key, value);
}
}
Currently all this really seems to do is transform the camera view, which isn't what I wanted at all (I have actually quit using it in my game because of its effect).
Not sure if anyone knows how to accomplish this, but if you have done it, or know of a way to do it that would be great.
------------------------------------------------------------------------
Second question (aka the "and more"):
I was attempting to figure out how to get communication between elements working with an Event model, however I either haven't found the event model that Unity implements to do custom events, or they don't have one at this point (which would be a little dissappointing).
So, through a little searching online I managed to find some code which appears to work like an Actionscript 3 event model (though, with some changes). I ported it to Javascript for anyone who wants to use it:
(Original)
#pragma strict
public interface CEventListener{
function HandleEvent(evt:CEvent):boolean;
}
public interface CEvent{
function GetName():String;
function GetData():System.Collections.Hashtable;
function SetData(key:String, value):void;
}
public class EventManager extends MonoBehaviour{
public var LimitQueueProcessing:boolean = false;
public var QueueProcessTime:float = 0.1;
private static var instance:EventManager = null;
private var listenerTable:System.Collections.Hashtable = new System.Collections.Hashtable();
private var eventQueue:Queue = new Queue();
public static function getInstance():EventManager{
if(instance == null){
var go:GameObject = new GameObject("EventManager");
instance = go.AddComponent(typeof(EventManager)) as EventManager;
}
return instance;
}
public function AddListener(listener:CEventListener, eventName:String):boolean{
if(!listener || !eventName){
Debug.Log("EventManager - AddListener faired, no listener of event name specified");
return false;
}
if(!listenerTable.ContainsKey(eventName)){
Debug.Log("ADDING EVENT '" + eventName + "' NOW");
listenerTable.Add(eventName, new System.Collections.ArrayList());
}
var listenerList:System.Collections.ArrayList = listenerTable[eventName] as System.Collections.ArrayList;
if(listenerList.Contains(listener)){
Debug.Log("Listener already exists");
return false;
}
listenerList.Add(listener);
Debug.Log("Event added successfully");
return true;
}
public function DetachListener(listener:CEventListener, eventName:String):boolean{
if(!listenerTable.ContainsKey(eventName)){
return false;
}
var listenerList:System.Collections.ArrayList = listenerTable[eventName] as System.Collections.ArrayList;
if(!listenerList.Contains(listener)){
return false;
}
listenerList.Remove(listener);
return true;
}
public function DispatchEvent(evt:CEvent):boolean{
var eventName:String = evt.GetName();
if(!listenerTable.ContainsKey(eventName)){
Debug.Log("Event has no listeners");
return false;
}
var listenerList:System.Collections.ArrayList = listenerTable[eventName] as System.Collections.ArrayList;
var i:uint;
var length:uint = listenerList.Count;
for(i = 0; i < length; i++){
if((listenerList[i] as CEventListener).HandleEvent(evt)){
break;
}
}
return true;
}
public function QueueEvent(evt:CEvent):boolean{
if(!listenerTable.ContainsKey(evt.GetName())){
Debug.Log("QueueEvent failed due to not listeners for event");
return false;
}
eventQueue.Enqueue(evt);
return true;
}
public function Update():void{
var timer:float = 0.0;
while(eventQueue.Count){
if(LimitQueueProcessing && timer > QueueProcessTime){
return;
}
var evt:CEvent = eventQueue.Dequeue() as CEvent;
if(!DispatchEvent(evt)){
Debug.Log("Error while processing event");
}
if(LimitQueueProcessing){
timer += Time.deltaTime;
}
}
}
public function OnApplicationQuit():void{
listenerTable.Clear();
eventQueue.Clear();
instance = null;
}
}
So, I guess the question here is whether or not anyone knows of the event model that is actually meant to be used (assuming it exists)?
Thanks for your time.

New Topic/Question
Reply




MultiQuote





|