Wie kann man ein UI-Bild und seine untergeordneten Elemente gleichzeitig skalieren und drehen?C#

Ein Treffpunkt für C#-Programmierer
Guest
 Wie kann man ein UI-Bild und seine untergeordneten Elemente gleichzeitig skalieren und drehen?

Post by Guest »

Das UI-Bild ist ein Kind einer Leinwand, die auf den Weltraum eingestellt ist.
Das UI-Bild hat einige UI-Kinder wie Textmesh Pro, die ich mit dem Bild skalieren und drehen möchte.
Screenshot der Canvas-Einstellungen
Image
und Screenshot des Bildes Einstellungen
Image

Während es skaliert und rotiert, verlieren die untergeordneten Elemente des Bildes die Ausrichtung und verlassen den Bildbegrenzungsbereich und bleiben beim Verkleinern beim Bild hängen.
Außerdem wird alles ein wenig skaliert richtigen Punkt und nicht direkt Verkleinern, als würde sich die Position ein wenig ändern.
Image

Der Effekt, den ich erzielen möchte, besteht darin, das Hauptmenü zu animieren, indem es beim Drücken der S-Taste verkleinert und dann wieder in der Größe geändert wird.
Dies ist das Skript, das an die Bild-Benutzeroberfläche angehängt ist

Code: Select all

using System.Collections;
using UnityEngine;

public class ScaleAndRotate : MonoBehaviour
{
public GameObject objectToScale; // Object to scale and rotate
public float duration = 1f; // Duration for scaling and rotating
public Vector3 minSize; // Minimum scale size
public Vector3 maxSize; // Maximum scale size
public bool rotateObject = false; // Toggle rotation during scaling
public float rotationSpeed = 360f; // Rotation speed in degrees per second

private bool scaleUp = false; // Determines the scaling direction
private Coroutine activeCoroutine; // Keeps track of the active coroutine
private Vector3 startScale;
private Quaternion initialRotation; // Stores the initial rotation

private void Start()
{
// Store the initial rotation of the object
initialRotation = objectToScale.transform.rotation;

// Determine initial scaling direction based on the current scale
Vector3 currentScale = objectToScale.transform.localScale;
scaleUp = Vector3.Distance(currentScale, minSize) < Vector3.Distance(currentScale, maxSize);
}

private void Update()
{
// Toggle scaling direction with 'S' key
if (Input.GetKeyDown(KeyCode.S))
{
if (activeCoroutine != null)
{
StopCoroutine(activeCoroutine);
}

// Reverse scaling direction and restart the scaling and rotation
Vector3 targetScale;
if (scaleUp)
{
targetScale = maxSize; // Scale up to maxSize
}
else
{
targetScale = minSize; // Scale down to minSize
}

// Toggle the direction for the next press
scaleUp = !scaleUp;

// Start the coroutine
activeCoroutine = StartCoroutine(ScaleAndRotateOverTime(objectToScale, targetScale, duration));
}
}

private IEnumerator ScaleAndRotateOverTime(GameObject targetObj, Vector3 toScale, float duration)
{
float counter = 0f;
startScale = targetObj.transform.localScale;

Quaternion startRotation = targetObj.transform.rotation;

while (counter < duration)
{
counter += Time.deltaTime;

// Smoothly scale the object
targetObj.transform.localScale = Vector3.Lerp(startScale, toScale, counter / duration);

// Rotate the object continuously while scaling
if (rotateObject)
{
float rotationStep = rotationSpeed * Time.deltaTime;
targetObj.transform.Rotate(Vector3.up, rotationStep, Space.Self);
}

yield return null;
}

// Ensure the scale reaches the exact target at the end
targetObj.transform.localScale = toScale;

// After scaling down, reset the rotation only when scaling to minSize
if (!scaleUp)
{
targetObj.transform.rotation = initialRotation;
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post