- Home /
How to mimic Unity Scene Gizmo?
I am trying to build a product visualization platform inside Unity and I think that the Scene Gizmo type of control is best suited for the job. Any ideas how can I accomplish that in code?
Answer by gheeler · Aug 29, 2013 at 11:39 AM
Don't really see how vectrosity can help. The question is how to simulate the behavior of the scene gizmo (similar to the autodesk view cube). Basically lets say that I have a cube, when I click on the left side of the face the cube rotates 90deg to the left; same for right. Top side gets you top view. Also, all this motions happening for the cube needs to happen to the whole scene in game mode. No idea how to do this
my bad, thought it was the visual effects you were talking about
Answer by hellaeon · Aug 29, 2013 at 12:49 PM
Hi Kadrasko,
Im not sure of the example you gave but gizmos can be used to show paths or points in the editor for some items in your script. For example with waypoints you draw lines between some given points and say a sphere at each point:
public Vector3[] waypoints;
private float node_radius = 0.5f; // gizmo
public void OnDrawGizmos()
{
if(waypoints == null)
return;
for(int i=0;i< waypoints.Length;i++) {
Vector3 pos = waypoints[i];
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(pos, node_radius);
if(i>0) {
Vector3 prev = waypoints[i-1];
Gizmos.DrawLine(prev,pos);
}
}
}
Anything to do with the gizmos should be done in OnDrawGizmos(). It's also an excellent addition to any bezier script so you can see the effect before running it to generate bezier curves in a scene. Following this thread, I needed to simulate hanging wires all over my scene, and I could not see them before playing the game, so I added in some gizmo code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Bezier : MonoBehaviour
{
public bool draw = true;
public Transform pos_from; // start of curve
public Transform pos_to; // other end of curve
public Vector3 pos_pivot_1; // in relation to pos_from
public Vector3 pos_pivot_2; // in relation to pos_from
public int segments = 5;
private LineRenderer lineRenderer;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.useWorldSpace = true;
lineRenderer.SetVertexCount(segments);
}
void Update()
{
if(draw)
{
draw_curve();
draw = false;
}
}
void draw_curve()
{
Vector3 _from = pos_from.position;
Vector3 piv_1 = pos_from.position - pos_pivot_1;
Vector3 piv_2 = pos_to.position - pos_pivot_2;
Vector3 _to = pos_to.position;
Vector3 v;
for (int i = 0; i < segments; i++)
{
float point = 1.0f/segments*i;
v = CalculateBezierPoint(point, _from, piv_1, piv_2, _to);
lineRenderer.SetPosition(i,v);
}
v = CalculateBezierPoint(1, _from, piv_1, piv_2, _to);
lineRenderer.SetPosition(segments-1,v);
}
private void OnDrawGizmos()
{
Vector3 _from = pos_from.position;
Vector3 piv_1 = pos_from.position - pos_pivot_1;
Vector3 piv_2 = pos_to.position - pos_pivot_2;
Vector3 _to = pos_to.position;
Vector3 v = Vector3.zero;
for (int i = 0; i < segments; i++)
{
float point = 1.0f/segments*i;
v = CalculateBezierPoint(point, _from, piv_1, piv_2, _to);
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(v, 0.1f);
if(i>0) {
point = 1.0f/segments*(i-1);
Vector3 prev = CalculateBezierPoint(point, _from, piv_1, piv_2, _to);
Gizmos.DrawLine(prev,v);
}
}
Gizmos.DrawLine(v,_to);
}
Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1.0f-t;
float tt = t*t;
float uu = u*u;
float uuu = uu*u;
float ttt = tt*t;
Vector3 p = uuu * p0; //first term
p += 3 * uu * t * p1; //second term
p += 3 * u * tt * p2; //third term
p += ttt * p3; //fourth term
return p;
}
}
Hopefully these 2 examples can help get you on your way Cheers
Thanks, I looked at and tested the code, and even thought I cannot use it for this scene, it will prove really useful in the future, thanks again.
But, still, I didn't make myself understood. $$anonymous$$y bad. I want to make a visualization project and I need to have a GUI element in the game, who's functionality mimics the one of the scene gizmo Click on the cones- the scene rotates, click on the cube-changes cameras. Plus the scene gizmo rotates with the camera.
ahhhh ok, a similar question was asked previously with no response. There is a way though! Try some of the methods outlined in this link here. It goes into detail about adjusting the scene editor, the section you will want to look at is Setting the position and rotation directly This should get you on your way. Love to know how it goes, so let me know.
Yap, I checked that out, and also I am looking at the SceneViewRotation decompiled from the Unity.dll but its a lot I don't understand. I'll keep you posted when I get something working
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I make a Game Object invisible? 1 Answer
Camera Effect + Translating US to C# 3 Answers
I want to put main camera floating 0 Answers
Natural camera sway 1 Answer