版本:Unity 6 (6000.0)
语言English
  • C#

Transform.localScale

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实阅读了用户提出的每项修改建议,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的修改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册
public Vector3 localScale;

描述

相对于游戏对象父对象的变换比例。

以下示例创建一个比例为 (1,1,1) 的球体 GameObject。然后,应用程序将 Transform.localScale 从 1.0 更改为 0.25,然后再反复更改回 1.0。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour { private GameObject sphere; private Vector3 scaleChange, positionChange;

void Awake() { Camera.main.clearFlags = CameraClearFlags.SolidColor;

// Create a sphere at the origin. sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = new Vector3(0, 0, 0);

// Create a plane and move down by 0.5. GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.position = new Vector3(0, -0.5f, 0);

// Change the floor color to blue. // The blue material is present in Resources and not created in this script. Renderer rend = plane.GetComponent<Renderer>(); rend.material = Resources.Load<Material>("blue");

scaleChange = new Vector3(-0.01f, -0.01f, -0.01f); positionChange = new Vector3(0.0f, -0.005f, 0.0f); }

void Update() { sphere.transform.localScale += scaleChange; sphere.transform.position += positionChange;

// Move upwards when the sphere hits the floor or downwards // when the sphere scale extends 1.0f. if (sphere.transform.localScale.y < 0.1f || sphere.transform.localScale.y > 1.0f) { scaleChange = -scaleChange; positionChange = -positionChange; } } }