【问题标题】:Unity2D: Scaling image in scene windowUnity2D:在场景窗口中缩放图像
【发布时间】:2016-09-01 20:14:30
【问题描述】:

我有这个脚本:

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++];
}

谢谢你:)

【问题讨论】:

    标签: unity3d scaling


    【解决方案1】:

    声明另外 2 个变量 最后宽度 最后一个高度

    在 Update() 函数中执行类似的操作

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

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

    【讨论】:

    • 感谢您的回复,但我收到此错误:错误 CS1525:意外符号“this”。它链接回 this.lastWidth = width;
    • @Bakausagi-chan 你必须把;放在setSize(this.width,this.height)之后。仅供参考,如果他们解决了您的问题,您真的需要开始接受答案。这是一种表示感谢的方式。您在 SO 上得到了很多帮助,但您似乎不接受其中任何一个,即使是那些似乎已经解决了您的问题的。
    • 呃,再次抱歉,但现在我收到此错误:成员 `Spritechange.setSize' 不能用作方法或委托
    • 发布您的 setSize 代码。如果已经有一个 setSize 成员,则将函数名称更改为其他名称,例如 ResizeMe 或其他名称,然后在那里调整大小。我目前无法打开项目以提供有保证的工作示例:S
    • 我想我只是将 setSize 设为浮点数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多