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

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.

Related

Override vectors in the inspector

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 :)

How do I make a 3D bounding box on a SkinnedMesh object in Unity?

I need to make a 3D bounding box around a SkinnedMesh renderer object for Unity. I need to print the vertices to the screen or a JSON file for export. Would you happen to know the key steps or even offer a code snippet for the bounding box part. I can figure out how to get the data out of the engine.
Thanks for the help!
Brian
I've looked around the documentation and found out that the SkinnedMeshRenderer class already has the bounds property, meaning that you probably only need to access it without having to create it.
Also in the Mesh.bounds and Renderer.Bounds documentations, there are a couple of snippets I belive could be helpful.

Unity: Add Button Script to 3D gameObject

I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
You could do it by using OnMouseDown() function. Here is the link Unity Docs
You will also need to add the image script along with the button script.

Unity3d do something when attached a script to gameobject

My problem is, when I attached my script to a game object, I want to make some calculation and add Edge Collider to this game object. Is there any event like "OnAttached" or something else?
Thanks for all your helps.
Thanks to Ruben I found the solution. RequireComponent is what I was looking for, but I actually needed an event like "OnAttached" and at last I found. It is "void Reset()".
The automated addition of the Edge Collider can be done by using [RequireComponent(typeof(EdgeCollider))]
Learn more here:
https://docs.unity3d.com/ScriptReference/RequireComponent.html
Edit: OP says this wasn't helpful, refer to his comment below for more information:
You then can simply put all necessary calculations into the Start() functions, which get called, when a script is enabled.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

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.

Categories