Created
May 14, 2013 09:04
-
-
Save s2kw/5574660 to your computer and use it in GitHub Desktop.
Usage :: Attach to UIPanel(NGUI) Object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent (typeof(UIPanel)) ] | |
public class PointingBillboard : MonoBehaviour { | |
public GameObject TargetObj; | |
public Vector3 Offset; | |
private Vector3 _latestOffset; | |
private Vector3 _targetPos; //GameObject's position. | |
private Vector3 _targetLatestPos; | |
private Camera _2DCam; | |
private Camera _3DCam; | |
/* FORCE Stay current position */ | |
private bool _forceStay; | |
public bool forceStay{ set{this._forceStay = value;} } | |
// Use this for initialization | |
void Start () { | |
_2DCam = GetMyCamera(transform); | |
_3DCam = Camera.main; | |
} | |
/*再起的に親オブジェクトにカメラが無いか確認。あればカメラを返す。*/ | |
Camera GetMyCamera(Transform tr){ | |
GameObject pr = tr.parent.gameObject; | |
Camera cam = pr.camera; | |
if(cam != null){ | |
Debug.Log("cam:" + pr.gameObject.name); | |
return cam; | |
}else if( pr != null ){ | |
return GetMyCamera(pr.transform); | |
}else{ | |
Debug.Break(); | |
Debug.LogWarning("NGUI Camera doe's not found."); | |
return null; | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
SnapToTargetGO(); | |
} | |
void SnapToTargetGO(){ | |
Vector3 targetObjPosAt2D = _3DCam.WorldToScreenPoint(TargetObj.transform.position); | |
// Vector3 targetObjPosAt2D = _3DCam.WorldToScreenPoint(TargetObj.position); | |
/* if nothing between latest position and current position, do nothing. */ | |
if( targetObjPosAt2D == _targetLatestPos && | |
Offset == _latestOffset) return; | |
_targetLatestPos = targetObjPosAt2D; | |
_latestOffset = Offset; | |
Vector3 posAt2DWorld = _2DCam.ScreenToWorldPoint(targetObjPosAt2D); | |
transform.position = new Vector3(posAt2DWorld.x + Offset.x,posAt2DWorld.y+Offset.y, transform.position.z); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
[CanEditMultipleObjects] | |
[CustomEditor (typeof(PointingBillboard))] | |
public class PointingBillboardInspector : Editor { | |
public override void OnInspectorGUI(){ | |
var pb = target as PointingBillboard; | |
pb.Offset = EditorGUILayout.Vector3Field("Offset", pb.Offset ); | |
pb.TargetObj = EditorGUILayout.ObjectField("target",pb.TargetObj,typeof(GameObject)) as GameObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment