How to make transparent UI visible in Unity Editor? - unity3d

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.

Related

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

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.

Property "Raycast target" does not work in animation

TL;DR - Property "Raycast target" (from Image in Canvas) is successfully animated but has no real effect (passes events through itself when it should not).
I have Image in Canvas that I am animating. In animation I change "Raycast target" property so I can interact with buttons behind after the animation ends.
Propery changes in inspector, but has no effect - I can press buttons behind when animation hasn't finished yet and "Raycast target" is "True".
When I do not use animation and change propery directly through script - everything works as expected.
What am I missing?
My Scene:
Canvas
|-Buttons
|-Image (Foreground, Animated)
My animated Image:
My Animation:
Note: "Write Defaults" in animator does not affect anything.
Unity 2020.3.8.f1 LTS
I think raycastTarget just enables Unity to consider this object for raycasting events. Turning it on or off may not make any difference to the elements behind it. I am not sure of it without testing but they have stated a single line for the raycastTarget property in the Documentation which does not help too much about this case.
I am sure that there is a blockRaycasts property on CanvasGroup component in Unity. Add this component to the object and it should block the elements behind it if it has a graphic (Image or something) attached to it.
I have not found solution, so I fixed problem through script. It checks animator state and manually changes property. In my case I have "Idle" state that is active when animation is not playing.
public class RaycastTargetFix : StateMachineBehaviour
{
public string interactableState = "Idle";
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
var image = animator.GetComponent<Image>();
if (image) image.raycastTarget = !stateInfo.IsName(interactableState);
}
}
Script needs to be added to animator:
Of course raycastTarget property should not be animated, otherwise it cannot be changed through script.

Animation auto reset when scrolling up and down in Unity

I have create an apps with a timeline like where the function is when scrolling up and down, the content should be animated. I want to build my apps just like this page (http://www.bbc.com/future/bespoke/20140304-how-big-is-space-interactive/ ).
I've put 2 trigger box on top and bottom of the canvas. 2 Animations were created, so when the content from below hit the trigger box when scrolling, it should be animated to large size (animator 'planettolarge'). So does when the content hit the trigger box at the top, it should be back to smaller size back (animator 'planettosmall').
enter image description here
The problem is, how to set the animation to be large size mode when the content is center on the canvas when scrolled from top. Because the animation is normal when the page is scrolled from the bottom.
Here is the simplest I have tried so far since I'm a beginner in Unity. I would appreciate so much if you drop any solution here. TQ =)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trigger : MonoBehaviour
{
Animator anim_roket;
Animator anim_planet;
private BoxCollider2D triggerup;
private BoxCollider2D triggerdown;
void OnTriggerEnter2D(Collider2D col)
{
switch (col.tag)
{
case "triggerup":
GetComponent<Animator>().Play("planettosmall");
break;
case "triggerdown":
GetComponent<Animator>().Play("planettolarge");
break;
}
}
What i recommend is to use the animator component and triggers instead of two animations, when you reach your trigger point you fire the animation trigger and the animation starts running.
first create an empty state and set many transitions to each f your animations with a bool parameter, if any scripts sets or clears that parameter the animation will be fired or stopped, remember to reset the animation time to the beginning every time you stop any animation so it can start from the beginning again.
see this tutorials to get a better idea, I hope it helps
https://www.youtube.com/watch?v=JeZkctmoBPw
https://www.youtube.com/watch?v=s7EIp-OqVyk

How to change cameras in Unity based on camera coordinates?

I'd like to create a movie in Unity, so I would need several cameras and camerapaths.
On the top of this off course I'd like to change between them. For example if Camerapath1 reaches a significant point with Camera1 then I'd like to change to Camerapath2 with Camera2, etc.
I also have Camera Path Animator asset installed. It's working perfectly when I'm using it with only one camera for several camerapaths but I'm unable to change between maincameras.
I'm a newcomer to Unity. I also know that I should do something like this:
...
camera1.camera.active = false;
camera2.camera.active = true;
...
...but where should I populate these lines? On the top of this, how may I catch the event when a camera on a specific camerapath reaches a particular point?
The way to go would be an animation controller that has all camera as children and controls the active state of all cameras. This provides perfect control over the behaviour.
Create an empty game object, add all cameras as children, add an Animator to the main object with one animation. This animation takes all the camera and set their active state. One extra bonus of this approach is the possibility to call methods as well using the AnimationEvent process. You can still define within the animation for some triggered actions like explosions or movements of objects.
As I said, this give you perfect control since you can define easily actions at specific time.
The downside of it is the rigidity of the process. It may not be as flexible as code, but since you are making a movie, you probably do not need flexibility.
If so, you would have your cameras with a collider and rigidbody (isKinematic true), then you would have some trigger box with a simple script:
public void CameraTrigger:MonoBehaviour{
public GameObject nextCamera;
void OnTriggerEnter(Collider col){
if(col.gameObject.CompareTag("Camera")){
col.gameObject.SetActive(false);
nextCamera.SetActive(true);
}
}
}
Then you drag the camera meant to start next as nextCamera.

Unity3d 3d skybox not quite right

I'm messing around in unity3d in order to learn it.
Had a crack at making my own 3d skybox like in source engine for example. I'm using the standard 1st person controller.
I made another camera with same FOV for my skybox, and slaved it to the camera in the 1st person controller using the script below which I put on my skybox camera.
(Maincam field has the 1st person controller camera component in it)
using UnityEngine;
using System.Collections;
public class CameraSlave : MonoBehaviour {
public Component Maincam;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.rotation = Maincam.transform.rotation;
}
}
You can see result here. Its a bit funny. (The big tetrahedron shape in the background is in my skybox everything else is normal)
As far as I understand it as long as the camera fov is the same it doesnt matter what size my skybox things are.
I think the problem, is there is some lag maybe? Like the Update in the code above is being called one frame too late? I tried calling that update from the 1st person controllers mouse look script but as well as getting loads of errors the result was the same.
I can't visualize your example, btw:
I think the problem, is there is some lag maybe? Like the Update in
the code above is being called one frame too late? I tried calling
that update from the 1st person controllers mouse look script but as
well as getting loads of errors the result was the same.
You can't rely on the order in which Update method will be called by the engine (unless you force a particular order, but this isn't generally a good choice). For camera update operations it's better to use LateUpdate. It's guaranteed that it will be called after all Update methods.