How do I get all timelines present in the scene of Unity and add them to my list?
private List<PlayableDirector> timelines = new List<PlayableDirector>();
timelines = FindObjectsOfType<PlayableDirector>().ToList();
Related
I am new to 2d animation in unity and wanted to write a script that determines the direction my character is facing based on what animation is playing. My animations are all called something like idleL or walkingL. At the moment I looping through all of my animations to determine which way the character is facing with AnimatorStateInfo(0).IsName.Would it be possible to just determine if the animation ends in the characters L, R, U or D using this function?
Any answers are greatly appreciated.
You can acces the clip name by using GetCurrentAnimatorClipInfo. You can see how its done in the example in the docs.
//Get them_Animator, which you attach to the GameObject you intend to animate.
m_Animator = gameObject.GetComponent<Animator>();
//Fetch the current Animation clip information for the base layer
m_CurrentClipInfo = this.m_Animator.GetCurrentAnimatorClipInfo(0);
//Access the Animation clip name
m_ClipName = m_CurrentClipInfo[0].clip.name;
In Unity, we can get the material that the GameObject has with the following code.
Material myMaterial = GetComponent<Renderer>().material;
But with the above code, we can only get one material per GameObject.
However, in actually, Unity GameObjects can have more than one material.
As shown in the image below
There can be more than one material per GameObject if it is assigned on a face-by-face basis.
I tried to get multiple materials with the code below but it didn't work.
List<Material> myMaterials = GetComponent<Renderer>().material;
Is there a way to get multiple materials assigned to a GameObject?
You can use the Renderer.Materials: https://docs.unity3d.com/ScriptReference/Renderer-materials.html
List<Material> myMaterials = GetComponent<Renderer>().materials.ToList();
I am currently creating a 2D-game in Unity and facing troubles in level design. I would like to create about 100 levels, each with different prefabs at different positions.
In order to load up the proper levels I have built an architecture with scriptable objects. Tilemaps are being used to represent obstacles. So it is possible to have about 30 different tile-positions for each level. It seems wrong to me to fill in those informations on every scriptable object seperatly.
What I am now looking for is a way to create a level in the editor and save the data directly in a scriptable object. To have a button in editor which says: "Save current scene-layout in e.g. scriptable object level 3". And also being able to load every level to the scene in editor mode.
You create your level in a prefab and reference this prefab into your ScriptableObject.
So your level prefab contains your tilemap and other prefabs.
I would suggest Raphael solutions, but in some cases this can be hard if you are deal with prefabs issues.
Other way to achive this would be create your custom editor to iterate your scene and create this ScriptableObject.
I use this code for something similar
GameObject __PARENT = GameObject.Find("__PARENT");
Vector3 centroid = Vector3.zero;
Transform[] childs = __PARENT.transform.GetComponentsInChildren<Transform>();
foreach (Transform go in childs)
{
Debug.Log(go.name);
centroid += go.position;
}
centroid /= (__PARENT.transform.childCount);
GameObject centerPivotObject = new GameObject();
centerPivotObject.name = "CenterPivotObject";
centerPivotObject.transform.position = centroid;
foreach (Transform go in childs)
{
go.parent = centerPivotObject.transform;
}
In my case I was trying to center pivot in parent object, but you can combine this iterate over your parent game using this and create using this your ScriptableObject
https://wiki.unity3d.com/index.php/CreateScriptableObjectAsset
I am creating an AR application with Unity and Vuforia. Right now an object is placed every time the user taps on the screen. I know it is possible to create the object only once and move it every time the user taps on the screen by unchecking the "Duplicate Stage" option but what I am looking for is to place only 2 instances of the object maximum. This means that when the user taps for the 3rd time, the object created in first is deleted and a new one is created. As I am new to Unity and Vuforia I would need some help doing it. Thank you !
Couple ways to do it. Using a list can provide some flexibility and scalability. Some example code:
List<GameObject> myObjects = new List<GameObject>();
if (Input.GetTouch(0).phase == TouchPhase.Began) // when user touches screen
{
myObjects.Add(SpawnObject()); //your method to spawn and return the spawned Gameobject to add to the list
if (myObjects.Count > 2)
{
Destroy(myObjects[0]); // destroy the gameobject
myObjects.RemoveAt(0); // remove from list
}
}
How can I programmatically change the default scene in Unity?
Currently, I'm only able to change the default scene via the Unity editor...
I want to change default scene so that the next time the user enters the app it load another scene.
Any help is appreciated.
You can set, as the initial scene, an empty one, that just loads another scene (based on a previously saved value) as soon as it starts. As an example, you can retrieve this value using PlayerPrefs, as it:
void Start(){
int defaultLevel = PlayerPrefs.GetInt("defaultLevel");
Application.LoadLevel(defaultLevel);
}
SceneManager.LoadScene(0); //Use scene index
SceneManager.LoadScene("sceneName"); //Use scene name
http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
Make sure to add the scenes to the Build Settings before trying the code above.