查看: 1447|回复: 1
打印 上一主题 下一主题

untiy3d脚本系列知识1-Unity3d javascript native array base redim

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-3 17:43:07 |只看该作者 |倒序浏览
In my recent pathfinding glory and javascript discoveries I unearthed the native array which is very different from the Array type. First off the Array type will never show objects in the inspector. If for example you have a class with some simple variables like the following :
class Custom extends Object

{

    public var p1:Vector3;

    public var p2:Vector3;

}
When you declare an Array and you Push values of class Custom inside it, you will not be able to edit it like you do with animation arrays and materials. Both of the last two are what is called builtin arrays. The unity3d documentation specify that builtin arrays can easily precess 2 millions vector3 in 1 second. While this speed is nice they could have given it a little bit more power, like resizing …
Resizing a javascript native array
Like always, this is an exploratory example and you may find other more optimized ways to do it, but this is pretty much the most basic form of resizing.
class Graph extends Object

{

        public static var nodes:Node[];

        public static var builtinAllocBlock:int = 30;

        public static var nodeCount:int = 0;



        public function Graph()

        {}



        function CheckToReallocBuiltinNode()

        {

                //Check if we potentially hit the end of the array

                var sizeCheck:int = ((nodeCount + 1) % builtinAllocBlock);

                Debug.Log("Size check - count: " + nodeCount + " sizecalc: " + sizeCheck);

                if( sizeCheck  == 0 )

                {

                        var newSize:int = (((nodeCount + 1) / builtinAllocBlock) + 1) * builtinAllocBlock;

                        Debug.Log("Reallocation of node builtin array from size: " + nodeCount + " to: " + newSize);

                        //We need a more space.

                        var temp: Node[] = new Node[newSize];



                        //***de copy

                        for(var i:int = 0; i < nodeCount; i++)

                        {

                                temp = nodes;

                        }



                        //Overwrite old

                        nodes = temp;

                }

        }



        function Clear()

        {

                nodes = new Node[builtinAllocBlock];

                nodeCount = 0;

        }

}
Now with the above code you would have to call Clear() to initialize for the first time the array. This is because Unity tends to call the cons***ctor of MonoBehaviour when you switch from the app to another app (code editing for example). This would lead to the graph being always re-initialized. In any case study the above CheckToReallocBuiltinNode. Its a simple case of if you attain the next maximum reallocate and recopy.
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-8-3 17:54:43 |只看该作者
我爱纳金网~www.narkii.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-1-31 20:47 , Processed in 0.062781 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部