I want to have a clean animation coded per a script and therrfor i create two ui images, and alll works fine besides the images are displayed in bottom left corner. I tried to move it with the help of all Functions Unity gives us for (expect Move Towards because i dont want a animation inside of it). What am I doing wrong?
using System.Collections;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneController : MonoBehaviour
{
public SceneAsset scene;
public Sprite maskSprite;
public Material maskMaterial;
public Material backgroundMaterial;
private float time;
private void Start()
{
StartCoroutine("AnimationController");
}
private Image createMesch(string name, Material material) {
GameObject child = new GameObject(name);
child.transform.SetParent(gameObject.transform);
Image childMaterial = child.AddComponent<Image>();
childMaterial.material = material;
// Copied from the internet. Should Center the Pivot and anchor
childMaterial.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
childMaterial.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
childMaterial.rectTransform.pivot = new Vector2(0.5f, 0.5f);
return childMaterial;
}
private IEnumerator AnimationController() {
Image mask = createMesch("Transition_Mask", backgroundMaterial);
mask.sprite = maskSprite;
Image background = createMesch("Transition_Background", maskMaterial);
//Doesnt work
background.rectTransform.position = Vector3.zero;
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
print(background.rectTransform.position);
background.rectTransform.sizeDelta = screenSize;
background.color = Color.black;
float maskSize = Mathf.Sqrt(Mathf.Pow(Screen.width, 2) + Mathf.Pow(Screen.height, 2));
Vector2 targetSize = new Vector2(maskSize, maskSize);
// yield return new WaitForSeconds(5);
while (time < 1)
{
mask.rectTransform.sizeDelta = Vector2.Lerp(targetSize, Vector3.zero, time);
yield return null;
time += Time.deltaTime / 0.8f;
}
yield return new WaitForSeconds(2);
time = 0;
while (time < 1)
{
mask.rectTransform.sizeDelta = Vector2.Lerp(Vector2.zero, targetSize, time);
yield return null;
time += Time.deltaTime / 0.8f;
}
}
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(scene.name);
}
}
2