- Home /
Look at scene view camera
hi , I found a code that gives you the position of scene view camera to be looked at:
gameObject.lookAt(SceneView.lastActiveSceneView.pivot);
but I want to call this script when my scene views camera's pivot changed in editor (not in play mode). I found [InitializeOnLoad] to call function every frame in scene view but I'm writing my scripts in javascript. could someone help me with that?
Comment
[]
is just the C# syntax for descriptors. They work the same in Javascript using the @
symbol
Best Answer
Answer by Apocx79 · May 23, 2014 at 01:53 PM
Took me some time to figure it out and i have seen many posts regarding this problem. So ... be my guests ;P
// Author:
// Stefan Guntermann apocx@gmx.de
//
// Description:
// Attach Label Element to a GameObject.
// Use:
// gameObject.BroadcastMessage("setText", "foo");
// From: Parent GameObject
using UnityEngine;
using UnityEditor;
using System.Collections;
[ExecuteInEditMode()]
public class LabelMe : MonoBehaviour {
public bool face_MainCam = false;
public bool hideWhenInvisible = false;
public TextMesh t;
// Use this for initialization
void Start () {
t = gameObject.GetComponent(typeof(TextMesh)) as TextMesh;
t.alignment = TextAlignment.Center;
setText(transform.position.x.ToString() + ", " + transform.position.z.ToString());
}
public void setText (string text) {
t.text = text;
}
// Update is called once per frame
void Update () {
if (face_MainCam) {
// the Label is facing in the way of the camera
//transform.LookAt( Camera.main.transform );
if( SceneView.lastActiveSceneView != null ){
//print(SceneView.lastActiveSceneView.pivot);
transform.LookAt( SceneView.currentDrawingSceneView.pivot );
}
else {
//print(Camera.main.transform.position);
transform.LookAt( Camera.main.transform.position/*, -Vector3.up*/ );
}
// Flip the Label so be readable
transform.rotation = transform.rotation * new Quaternion(0,180,0,0);
}
}
}