Unity2D:在场景窗口中缩放图像

时间:2016-09-01 20:14:31

标签: unity3d scaling

我有这个脚本:

public Sprite[] Images; //Index starts at one because we are setting the first sprite in Start() method
public int _Index = 1;
public float width;
public float height;


void Start(){
    //Set the image to the first one
    this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
}

public void onClick(){
    //Reset back to 0 so it can loop again if the last sprite has been shown
    if (_Index >= Images.Length)
        _Index = 0;

    //Set the image to array at element index, then increment
    this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[_Index++];
}

完美地工作,它做的是它改变了图像和对象OnClick一旦所有图像被显示它循环。但是,我无法在场景窗口中手动缩放图像,也不能通过脚本。是否有更简单的方法来更改我的脚本,以便能够在我的场景窗口中手动缩放我的图像。 (我宁愿在我的场景窗口中扩展我的图像,而不是通过脚本,因为我会发现它更容易)请和谢谢你:)

第二次修改 这就是我的脚本现在的样子:

public Sprite[] Images;
//Index starts at one because we are setting the first sprite in Start() method
public int _Index = 1;
public float width;
public float height;
public float lastWidth;
public float lastHeight;

void ResizeMe()
{
    this.width = 1.567892f;
    this.height = 1.07f;
}

void Update()
{
    if(this.width != lastWidth || this.height != lastHeight)
    {
        ResizeMe (this.width, this.height);
        this.lastWidth = width;
        this.lastHeight = height;
    }
}

void Start(){
    //Set the image to the first one
    this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
}

public void onClick(){
    //Reset back to 0 so it can loop again if the last sprite has been shown
    if (_Index >= Images.Length)
        _Index = 0;

    //Set the image to array at element index, then increment
    this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[_Index++];
}

谢谢:)

1 个答案:

答案 0 :(得分:0)

再声明2​​个变量 lastWidth lastHeight

Update()函数中的

执行类似

的操作
if(this.width != lastWidth || this.height != lastHeight)
{
    setSize(this.width,this.height)
    this.lastWidth = width;
    this.lastHeight = height;
}

创建一个setSize函数来进行实际的大小调整。 现在,当你从编辑器更改公共宽度,高度变量时,更新将选择更改并运行调整大小脚本