Giter Site home page Giter Site logo

Comments (7)

ZeroChiLi avatar ZeroChiLi commented on July 28, 2024

重现方法:

using Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test2 : MonoBehaviour
{
    int a = 0;
    public Text textCom;
    public Text textCom2;
    public bool trigger = false;
    Color upColor = new Color(0, 0, 0, 0.001f);

    void Update()
    {
        ++a;
        using (zstring.Block())
        {
            textCom.text = (zstring)a;
            trigger = !trigger;
            textCom.color = trigger ? textCom.color + upColor : textCom.color - upColor;
        }

        if (Time.frameCount % 60 == 0)
        {
            using (zstring.Block())
            {
                textCom2.text = (zstring)a;
                trigger = !trigger;
                textCom2.color = trigger ? textCom2.color + upColor : textCom2.color - upColor;
            }
        }
    }
}

观察两个UI文本组件,上面显示的文本内容会完全一样。

大概也猜到原因了:zstring.get(int length) 这个方法,根据长度获取zstring,估计是第二次获取的时候,指向了同一个引用对象(前面刚用完那个)

from zstring.

871041532 avatar 871041532 commented on July 28, 2024

from zstring.

ZeroChiLi avatar ZeroChiLi commented on July 28, 2024

那只是我的演示,实际上是两个地方调用(⊙_⊙)

from zstring.

871041532 avatar 871041532 commented on July 28, 2024

把两处调用写在一个block里面,或者 .Intern() 一下吧

from zstring.

871041532 avatar 871041532 commented on July 28, 2024

如果在两个Update里代码不太好合到一处,又不想要 .Intern() 放入常驻缓存一直占用内存空间:
① 如果只是在Update范围内保证字符串正确性。可以考虑将using(){} 语法糖拆成两个,比如 Block.Begin() Block.End()。 在UI 统一Tick之前调用 Block.Begin(), 在UITick之后调用 Block.End()回收。
② 如果字符串只是需要在一段时间内常驻内存保持正确性。 可以参考.Intern() 加个 .InternVariant() 放入自定义缓存中,在恰当实时机手动clear一下。

from zstring.

ZeroChiLi avatar ZeroChiLi commented on July 28, 2024

用方法2 动态Intern解决了问题,感谢作者的回复 😆

from zstring.

ZeroChiLi avatar ZeroChiLi commented on July 28, 2024

还有种方法就是一个组件搞一个block 在修改文本前 一直拿着,要改再Dispose之前的block。

from zstring.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.