Override vectors in the inspector - c#

Is there a way to override how Vectors are displayed in the inspector ?
Let's say I want to add a button next to the vector fields, can I override the "Property Drawer" of Vector3 or should I create a new class/struct and apply a CustomPropertyDrawer on it ?
I've tried this, but it doesn't work.
[CustomPropertyDrawer(typeof(Vector2))]
public class Vector2Drawer : PropertyDrawer {
// overrides functions, but does nothing
}

I'd use the public Vector2 and a public bool for the button status for the sake of simplicity.
If you really want to achieve your editor custom behaviour then you need to check the unity Editor class.
With that you can use that class that is within unity itself and you can set up your own custom editor UI that communicate with the rest of the code of your application.
You can check regarding editor scripting: https://learn.unity.com/tutorial/editor-scripting#.
To add a button specifically check: Add button to Unity3d editor
As you can see the editor scripting is a disciplice of its own. I think its should be used in teams when a complex component will be used by a lots of people repeatedly soas to simplify its use.
I can understand that can be juicy to dive in if you are interested in learning unity deeply, but if you are building a game or have a launch of a product in mind, I would suggest to move on and simplify your editor setttings as much as possible and move on with your game. Take into account that it takes quite an effort to elaborate this cool UI component, and you might give up on launching your product by the time you got them as you'd like.
Hope that helps :)

Related

How can I make two animations work together in unity?

I'm making a first person shooter and im trying to make a reload animation.
I want to make it look like the hand grabs the mag and pulls it down.
How do I make two animations work together?
I remember having to do that for my wrestling game. What I did was create 2 controllers and 2 animations. Controllers should be the same (have same timing, transition and all that). Then make one animation for example animation of how hand reloads a gun. Now memorize sample rate and in which frame you put keys and what do they do (what do they change). Now start animating how mag is getting pulled BASED on what you have done previously. Like in what frame it should go down and in which frame it should be thrown or new one should be inserted and etc.
Then you will have to enter play mode in order to see them simultaneously. I am making a wrestling game and this method is what I use.
Good luck
https://docs.unity3d.com/Manual/AnimatorOverrideController.html
he Animator Override Controller is a type of asset which allows you to extend an existing Animator Controller
, replacing the specific animations used but otherwise retaining the original’s structure, parameters and logic.
This allows you to create multiple variants of the same basic state machine
, but with each using different sets of animations. For example, your game may have a variety of NPC types living in the world, but each type (goblin, ogre, elf, etc) has their own unique animations for walking, idling, sitting, etc.
By creating one “base” Animator Controller containing the logic for all NPC types, you could then create an override for each type and drop in their respective animation files.

Unity 3D Google Cardboard (VR) gaze functions without EventTrigger

I am currently working on some Unity VR project and I have a problem with gaze functions. As an example I will use Google VR DemoScene object named Cube. There is a Teleport script attached with some gaze code at the end:
public void OnGazeEnter() {
SetGazedAt(true);
}
public void OnGazeExit() {
SetGazedAt(false);
}
public void OnGazeTrigger() {
TeleportRandomly();
}
However this fragment seems to be useless and all gaze events are handled by EventTrigger component attached to the Cube object.
My question is - how do you handle gaze events (OnGazeEnter, OnGazeExit, OnGazeTrigger) with this code only? It would be way simpler not to attach EventTrigger component all the time.
They are not useless, just not very well documented how to use it. It tooks me a while for me to figure out how to.
If you want to use them, what you need to do is remove the EvenTrigger component, then select the Main Camera and Add the component called "GVRGaze" and now these events are fired by code (OnGazeEnter, OnGazeExit, OnGazeTrigger)
You can use the mask to filter layers if you want, this is very helpful.
Hope this helps you.

Create GameObject and save in Hierarchy

I would like to create gameObjects (text, 3d stuff, etc.) from scripts and save them in the scene.
For example: If I use a script like this,
public class ExampleClass : MonoBehaviour
{
public void Start()
{
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
}
}
the "plane" will be add in Hierarchy temporarily (just after you press start).
I'm looking for some way to create a lot of gameObjects automatically (through a script, api, etc.) and save them in the hierarchy.
you are talking of persistent GameObject after stopping scene i suppose.
The only possibility to make gameObject instance pop inside your scene hierarchy and stay after scene played and stopped is to use CustomEditors.
You have to know, unity editor is based on Unity Gui, so you can easily add buttons make it run some specific script coded by you, inside windows menu (for example).
But you can also custom the mouse right click on scene hierarchy to allow peoples to create instances of a allowed gameObject and add this created instance directly on child of previously clicked gameobject from the scene etc...
Or make create your own tools script, and add it as component on Gameobject and with specific layout for it and rendered by Unity Editor.
For use customEditor step by step :
1 - Create a folder named Editor inside Assets/ root project hierarchy
2 - Create a script like bellow, which is using reference to UnityEditor and add you could use the commented Attribute. At the end you have to make your class extend From Editor class.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
// uncomment this line bellow if you want to create customEditor script which you will be able to use on gameObject as component.
// typeof give script type , it's a script created as component and extending from Monobehaviour, he contain value etc.. he could be manipulated from this CustomEditor script via button who call Methodes.
//[CustomEditor(typeof(YourScriptComponent))]
public class MyNewCustomEditor : Editor
{}
for more examples of customEditors script look at unity doc: http://docs.unity3d.com/ScriptReference/Editor.html
3 - Create methodes to be used and called out of scene Playing mode.
you have to create buttons inside OnInspectorGUI() function which will call your methodes for instantiate inside scenes some GameObjects.
4 - WARNING , To instantiate resources like prefabs etc, you have to put yours needed resources inside folder called Resources and use inside your script the Resources.Load() methode.
see the doc for more information , it's pretty simple to use : http://docs.unity3d.com/ScriptReference/Resources.Load.html
Hope it will help, see You.
There was a similar question asked over here.
You want be making an Editor Script then using pretty much the same logic you would use at runtime. Here is the Unity Docs on editor scripts.
You can just make a C# script and put it in the "Editor" folder. If you want it to appear in the editor as a dropdown option you can throw in "[MenuItem("MyTools/CreateGameObjects")]" before a function as shown in the other Stack Overflow answer.

Drawing GUI stuff in scene view Unity3d

is there anyway to draw stuff on scene view without calling OnSceneGUI?
or is there a way to get OnSceneGUI to get called even if the object with the script attached is not selected?
Edit: Looks like I wasn't explicit enough on what I was asking for so here's a little update:
I have a whole bunch of controls shown as GUI objects on my Game Scene that are used by the game designers to more easily test the game. Since these tools are for development use rather than deployment, I wanted to move these to the scene window instead.
Unfortunately I am only able to display these GUI tools if the object that contains the script with the "OnSceneGUI" method is selected. Instead I want them to be displayed whenever the game is running and regardless of the selected object.
I have found the solution to my issue.
If you want your GUI stuff to always be displayed on the scene window you can do the following:
public class MyClass{
static MyStaticConstructor() {
SceneView.onSceneGUIDelegate += OnScene;
}
static void OnScene(SceneView sceneView) {
// Draw GUI stuff here for Scene window display
}
}
This code will run as soon as you press play and will draw whatever you wish on your scene window.
Hope it will help others!
There are many ways to use GUI,
The easiest but least efficient way is using the GUI class on OnGUI()
GUI.Label(new Rect(10,10,10,10), "This is a label");
GUI.Label(new Rect(10,10,10,10), "Your Texture2D here");
Any active monobehaviour will run OnGUI() if its defined. So it can be attached to any gameObject. You can create an empty gameObject in the scene and call it "GuiGameObject" for example and attach the script there. That way it wont be mixed in with your gameplay script.
There are also GUI textures --> More Info on GUITexture
Also I recommend checking out nGUI
Edit:
For OnSceneGUI You can try Editor.Repaint, you can use it to make sure that the inspector updates changes made inside of OnSceneGUI

Unity c# make 3D object as button to move to another scene

Im making a main menu, and I want to make the 3D object as a button to move to another scene. The 3D object is jar import from SKETCHUP. Im a newbie thats why im still dont know. can someone give me example code using c#?? the name of the object is JAR. thankyou in advance:)
This is how you do it:
Apply a Collider on your object (or you could make it with raycast but it's not necessary for menu buttons)
Define a OnMouseDown or OnMouseUp event. By the way, learn to use Unity documentation, it even has what you're looking for in this example: MonoBehaviour.OnMouseDown().
This is the code to handle click event: (taken directly from the provided link)
void OnMouseDown () { // or OnMouseUp
Application.LoadLevel("level name"); // or level index
}
I assume you know how to define different scenes so you can reference them at loading. If not, read this for a basic guide on how it's done: Application.LoadLevel.
Also a note: Unity has a really big community forum on it's own and has lots of active members which have much more experience. But please, do your homework and watch the tutorials and also try to search for your problem before spamming with questions that have already been explained. Nobody likes to help someone who doesn't want to do any effort on it's own.

Categories