Preview script generated gameobject in scene view when not in play mode - unity3d

I create an empty GameObject and attaching some scripts for generating the mesh and texture with several properties. How could I preview the the script result in Scene View when it's not in play mode? It's show nothing now.
And I also want the preview changed when I change the property value in Inspector.
Do I need to loop read the property value every second? This seems not a good idea.
So what is the general solution for my situation? I guess there must be a commonly used way for a generated GameObject.
Here is the example, I write a script to generate Cube mesh with x, y, z size. Now there are nothing show in the scene view, and when I change x, y, z value in inspector, nothing happened except I click play.
I want to see the preview in Scene View just like I right click and created a Cube GameObject provided by Unity.
This is my Cube's inspector:

By default, MonoBehaviours are only executed in Play Mode. By adding the ExecuteInEditMode attribute, any instance of the MonoBehaviour will have its callback functions executed while the Editor is in Edit Mode too. After that, you are able to check a value change in the Update()-Method.

Related

How to make transparent UI visible in Unity Editor?

BackGround
In Unity 2020LTS, I want to make a UI scene.
But in Game Panel, I discovered, although a animation is set at beginning (no conditions), the game will show what I see in Editor panel for a little time at first, then play the animation.
The StateMachine is Entry -> Target(Default)
I don't want show player what I see in editor, but only the first frame in animation.
I guess this is because loading level costs some time (almost 0.5 secs).
Question
So I try another way, make initial state of all objects be same as the first frame of animation.
This way work, seems just like it freeze at first frame for 0.5secs. However, I can't edit those objects visibly (Because they all are transparent in first frame).
I have tried Gizmos, but they don't work well. Besides, Gizmos makes me have to create lots of classes in C# scripts for each object, which just is component of animation and has no script.
Could there be any better way to show transparent (UI) object in editor scene only ?
Assuming you have some Game Manager script, you can add a GameObject, assigning it the UI element, and in the Start() function of the script, make it inactive, like so:
public class GameManager : MonoBehaviour
{
public GameObject menu;
void Start()
{
menu.SetActive(false);
//other statements
}
}
I'm not quite sure what you asked for but if you want to edit the UI elements that are invisible you can simply select them by using the hierarchy and edit them. I have linked an image with an example of this. Example scene with invisible panel
Image img;
// Start is called before the first frame update
void Start()
{
img = GetComponent<Image>();
img.color = new Color32(0, 0, 0, 0);
}
The code above will set the panels transparency to zero when the scene starts, make sure to use the using unity.UI namespace in order to acess the image component.

How to create a custom height snapping tool in Unity Editor

I am making a strategy game and I need to have a tool which places the objects above the terrain while I am dragging them in Unity Editor when I work on level design.
Basically I want to get result like here:
https://www.youtube.com/watch?v=YI6F1x4pzpg
but I need it to work before I hit the Play button in Unity Editor.
Here is a tutorial
https://www.youtube.com/watch?v=gLtjPxQxJPk
where the author of it made a tool which snaps the object to the terrain height when a key is pressed. I need the same to happen automatically whenever I place an object over my terrain. And I want my tool to adjust the Y position of the object automatically even while I am dragging it inside of the editor.
Also just to clarify: I don't need grid snapping and I don't need this functionality during the gameplay. I just need to have a tool for my level design work.
Please give me a clue where to start with it.
Thanks!
There is this tag you can apply to classes so they do call their regular events during editor mode already: https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
A trivial way then would be to apply this to a special class/object which regularly "finds" all objects from the game object hierarchy. Then it shall filter that list for the ones you want to snap to the axis and enforce their Y.

Unity deforms every prefab that I create

I have problem with Unity prefabs. Every time I create a nice looking UI in main scene and I want to store it as prefab for later usage if I open this prefab it's deformed(random cliping, moved UI elements in different directions, ... such a mess). If I try to fix this prefab in prefab editor to make it nice looking like before and then if I insert this back to scene it's totally unusable. It creates bigger mess than before in prefab editor... I don't know what to do. Editor throws no errors btw.
In first image is nice looking UI(example already created). In 2nd image is prefab editor. Text is totally out of panel and 3rd is from back in scene (means removed old one UI and place stored prefab from folder) the panel is gone.
Imgur - images are orded by scene to prefab editor and back to scene
On those images you can see a 3 elements... but if I make more complex UI, it's clipping through each other in prefab editor and also back in scene.
Unity version: 2019.1.7f1

How to prefab animations in Unity?

created a prefab for door and added the prefab doors to the scene. This prefab door has an animator. The problem is when I try to change the animator x,y,z position on the second door in the scene it changes the first door.
I tried duplicating the doors and changing the position x y z but it changed the animator for the first door.
I just have a script that play the animator.
The expected result should be that I can drag and drop doors as needed without creating individual animators for each door. It looks like i need to create animators per door there is and also this probably will require me to create new scripts per each door. It seems awkward since I should be able to add doors and just chaning the position x y z in the animator.
The problem is that their Animator component uses the same AnimatorController asset which uses certain AnimationClip asset references for it's states.
I don't know if there is an easier way for achieve but I would do the following:
For what you want you would have to copy the AnimatorController asset (CTRL+D) as well as the AnimationClip asset you want to change.
Then in the cloned AnimatorController click on the according State you want to change and in the Inspector reference the according cloned AnimationClip.
Now you have a copy of the AnimatorController using a different AnimationClip but with the same states, traditions etc. so you can edit both animations and controllers individually.
finally in the Animator component reference the according AnimatorController for each prefab.
The remaining problem maybe is now that for the future if you change the States, transitions and especially parameters you have to do it in both AnimatorController assets individually.
That is perhaps not because of an animator, but a specific behavior of animation in Unity. Animation animates transform property according to parent, as far as I understand. So the first parent of your prefab is captured, and its transform property is used for animation. But I'm not exactly sure about this explanation - the actual behavior is covered under the Unity's source code. A common solution for this that I found is to create a parent or container inside your object's prefab. I don't know why it works, but it works. Perhaps because the parent of your animated object is now always the same.
Parent can be just an empty game object. So you make your door prefab, place another empty game object into this prefab, make your door a child of this container, set 0 for all coordinates of your door according to container and for the container itself. Now you can add an animator component to your object, which you want to animate (not the container), and animate it inside the prefab scene. After that you can place the copies of this prefab (container + your animated object inside) wherever you want inside your game scene - all animations of copies will proceed independently.

2d ordering in unity

i am making a 2d game in unity.
i am missing a feature that cocos2d has that when you create something in the parent it keeps its z ordering inside that parent.
the problem that i am experiencing is with popups, when i create a popup or menu i want to make sure that it gets displayed above everything else in the scene or any other popup, but i cant make sure that it does because some elements in the scene may have z value that even though they are inside a parent object may appear above my popups if i am not careful. same goes for popups if i would like to have a popup on top of another popup i will have to make sure that there are no intersecting children between them.
does anyone know is there any way of containing an object to be rendered only inside its parent z order like cocos2d does? or should this problem be approached differently.
When working in 2D mode in Unity, you should just focus on setting the Order In Layer value of the sprites to have them display as you want. Leave all the z values in 0.