【问题标题】:UI.Image.pixelsPerUnitMultiplier doesn't update via codeUI.Image.pixelsPerUnitMultiplier 不通过代码更新
【发布时间】:2020-01-01 04:58:30
【问题描述】:

我正在制作一个神经网络建模程序,并且我已经为我的背景网格制作了一个平铺图像。

我正在尝试通过代码更改每个单位乘数的图像像素,但它不起作用(在运行时通过编辑器编辑它就可以了)

这是我的放大/缩小代码:

float desiredSize = this.GetComponent<Camera>().orthographicSize;
desiredSize -= Input.mouseScrollDelta.y * 3;

//Clamp between min and max
desiredSize = Mathf.Clamp(desiredSize, minSize, maxSize);

//Smoothly interpolates to desired size
this.GetComponent<Camera>().orthographicSize = Mathf.Lerp(this.GetComponent<Camera>().orthographicSize, desiredSize, 5f * Time.deltaTime);

//Change pixels per unit multiplier to half of camera's size (with only 1 decimal char)
gridImage.pixelsPerUnitMultiplier = Mathf.Round( (this.GetComponent<Camera>().orthographicSize / 2) * 10f) / 10f;

它按预期改变了每单位像素的值,但它似乎对图像本身没有任何影响。

【问题讨论】:

    标签: c# image user-interface unity3d pixel


    【解决方案1】:

    有趣的是,此属性无处记录。我什至在ImageEditorGraphcisEditor 的源代码中都没有找到它,所以现在我对实际负责显示该字段的内容很感兴趣^^

    然而,我发现在对尺寸设置进行更改后,编辑会调用EditorUtility.SetDirty(graphic);。当然,这在构建中不可用,但它让我寻找 - 我认为 - 正确的解决方案:

    更改属性后,您必须致电SetVerticesDirty

    gridImage.SetVerticesDirty();
    

    或者干脆SetAllDirty

    gridImage.SetAllDirty();
    


    虽然我不确定效率.. 你可能应该只在值实际改变时才调用它。

    还要避免所有那些重复的GetComponent 电话!在Awake 中执行一次,然后重新使用引用。

    你的Lerp终于没用了!由于您获得了当前大小并在该大小上计算了所需的大小,因此根本不会进行平滑缩放。而是在类中使用全局字段并更新该字段:

    // would even be better if you can already reference this in the Inspector
    [SerializeField] private Camera _camera;
    
    public Image gridImage;
    
    public float minSize;
    public float maxSize;
    
    private float desiredSize;
    
    private void Awake()
    {
        if (!_camera) _camera = GetComponent<Camera>();
    
        // initialize the desired size with the current one
        desiredSize = Mathf.Clamp(_camera.orthographicSize, minSize, maxSize);
    
        // do it once on game start
        UpdateImage(desiredSize);
    }
    
    // Update is called once per frame
    private void Update()
    {
        var currentSize = _camera.orthographicSize;
        desiredSize -= Input.mouseScrollDelta.y * 3;
    
        //Clamp between min and max
        desiredSize = Mathf.Clamp(desiredSize, minSize, maxSize);
    
        // is the scaling already done?
        // -> Skip unnecessary changes/updates
        if (Mathf.Approximately(desiredSize, currentSize)) return;
    
        //Smoothly interpolates to desired size
        _camera.orthographicSize = Mathf.Lerp(currentSize, desiredSize, 5f * Time.deltaTime);
    
        UpdateImage(_camera.orthographicSize);
    }
    
    private void UpdateImage(float size)
    {
        //Change pixels per unit multiplier to half of camera's size (with only 1 decimal char)
        gridImage.pixelsPerUnitMultiplier = Mathf.Round(size / 2 * 10f) / 10f;
    
        gridImage.SetVerticesDirty();
    }
    

    固定版本:

    “动画”末尾的这种轻微抖动来自您在分配 pixelsPerUnitMultiplier 时的舍入,您可能要考虑删除此舍入并简单地应用

    gridImage.pixelsPerUnitMultiplier = size / 2f
    

    【讨论】:

    • 我有 tru 所有的文档,也什么也没找到,但你的回答帮我解决了 ^^ 谢谢,你是最棒的
    【解决方案2】:

    Image.SetAllDirty();会使其工作,但以后可能会产生随机故障。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多