System.Diagnostics.Conditional Magical - the document on the code

  Recent work with UI deal more, various pit.

  Said today that magical System.Diagnostics.Conditional from the serialization function Prefab it.

  We do face the UI Obtaining various buttons, components will roughly two types of direct-sequence of the Prefab, the other is to get in your code: 

Serialization

= transform.Find tagObj ( "Child / Cube " ) .gameObject; // code gets

  Each has it's own benefits :

    Serialization is straightforward, do not write the code, and whatever you drag UI to which nodes can correctly quote. But the problem is also very obvious, after a few days you even get lost, there have been long-term maintenance experience people should be aware of.

    Code to get way from maintenance, it can make a direct look at the people know that the components on which node, easier to understand. But if node path is changed, will go wrong, and this in frequent changes of UI design on it is terrible especially if packaged obtain security function, it is more difficult to find the problem, such as:

    // packaged function, not linked to the wrong message 
    public  static the GameObject GetGameObject (the Transform from , String Find) 
    { 
        var Trans = from .Find (Find);
         IF (Trans) 
        { 
            return trans.gameObject; 
        } 
        return  null ; 
    }

  If From an efficiency standpoint, it should be serialized Prefab efficiency than the efficiency of use of the codes to find, because the sequence is a unique object ID, rather Transform sub-objects when the code is looking in the list, the Find List lookup function with efficiency is the same: 

  Here tagObj quoted by yourself, see ID serialized object is pointing above m_GameObject, it should be a GUID is internal. From an efficiency point of view slightly better, because I have come across thousands of Child in the previous project node, and then look for the following object node by thousands of database data obtained, it is simply sour cool efficiency, this demand is sure to Li times Transform all nodes are placed in Dictionary references ah, blood lesson...

  Second, the storage size:

    If the sequence of the Prefab which it uses as a reference ID, the increased storage size substantially a fixed value, but also for other after compression and packaging, the actual space is very small, and as it can be thermally more resources.

    If the code is written in C #, Find ( "XXX / ooo") inside the characters will be placed in a static field, which is terrible, because the end of the project not only due to excessive code, causing the program to more strings package is too large, we really encountered over IOS 50 Duo M single DLL problem limited ... of course, if the words written in Lua just as a resource, you can compile into a binary, you can compress everything Lua. but according to the general, "XXX / ooo" character usually longer particularly deep UI level than in the case ID, when the Chinese have even worse.

 

  Suppose we have Lua, you ask them to developers willing to write a lot of code Find, or simply a tow trouble? Because in the use of Lua is no different with C #, if you use a serialization anyone bother to go write code ... it really is a rare Unity official function more cattle than folk method exception.

  It would appear that serialization and really simple and efficient, but also save space, the main problem is in the post-maintenance, such as our variable called tagObj, object name made people may be cited Cube, absolutely no relevance on the name, in some under unpredictable circumstances if the reference is lost, even the original creators do not know what the situation is referenced object is much we can combine the two modes of words like, for example, add a comment in the components above: 

  Well, yes, there are better: 

  System.Diagnostics.Conditional can decide whether the conditions according to the compiler to compile, it is just a comment in a non-editor, the same is not added to the list, and in the editor you can get by GetCustomAttributes, then its usefulness is big : 

  1. Object of the check sequence editor is not lost, then the lost Attribute then automatically obtained by looking to loadPath.

  2. 在编辑器下检查序列化对象跟 loadPath 的对象是不是一致, 检查数据正确性.

  这些看似简单的功能, 在发布版本之前可能救不少人的命呢, 避免了每次抓人去祭天. 通过这个应用方法添加了一些信息, 并能提供上述功能支持.

 

测试一下 : 

 

-------- 编辑器 --------

 

-------- 发布后 --------

Guess you like

Origin www.cnblogs.com/tiancaiwrk/p/12103158.html