纳金网

标题: 向场景中动态加入物体(二) [打印本页]

作者: 会飞的鱼    时间: 2011-11-15 14:36
标题: 向场景中动态加入物体(二)



            Gettin’ Fancy
            






           If you’ve read our article Understanding Loops with Arrays, you might have a notion of how to use Loops and Arrays to instantiate an armada of Prefabs into your scene. Here’s some sample code:
         

           var myCube : GameObject;
         

            function Start()
         

           {
         

                // Store a bunch of different positions in an Array:
         

                var aPositions : Array = [new Vector3(0,0,0),
         

                                                 new Vector3(1,2,1),
         

                                                 new Vector3(2,1,1),
         

                                                 new Vector3(2,0,0),
         

                                                 new Vector3(0,3,0)];
         

            
         

                var rot : Quaternion = Quaternion.identity;
         

                // Loop as many times as there are elements in the aPositions Array:
         

                for(i=0; i<aPositions.length; i++)
         

                {
         

                     Instantiate(myCube, aPositions, rot); // Put a new cube on the screen using the i'th position  
         

                }
         

           }
         




           Here’s what it looks like.
         

           One More for the Road
           

           Just for kicks, here’s a script that’ll throw the cubes on the screen any old place, using Random.Range():
         

           var myCube : GameObject;  function Start() {      // Let's store some minimum and maximum possible x,y and z values      // to position our cubes:      var minX : int = -5;      var maxX : int = 5;      var minY : int = -5;      var maxY : int = 5;      var minZ : int = -5;      var maxZ : int = 5;      // (you can fiddle with these numbers to change the range of possible spawn positions)        var totalCubes : int = 50; // Change this to whatever number of cubes you'd like to have on-screen        var rot : Quaternion = Quaternion.identity;        for(i=0; i<totalCubes; i++)      {           // Use Random.Range to grab randomized x,y and z values within our min/max ranges:           var randomX : int = Random.Range(minX, maxX);           var randomY : int = Random.Range(minY, maxY);           var randomZ : int = Random.Range(minZ, maxZ);             var pos : Vector3 = new Vector3(randomX, randomY, randomZ);             Instantiate(myCube, pos, rot); // Put a new cube on the screen using a randomized position         } }
           






作者: 晃晃    时间: 2012-2-2 23:25
路过、路过、快到鸟,列位请继续...ing

作者: 菜刀吻电线    时间: 2012-2-17 23:19
凡系斑竹滴话要听;凡系朋友滴帖要顶

作者: 晃晃    时间: 2012-4-24 23:18
非常感谢,管理员设置了需要对新回复进行审核,您的帖子通过审核后将被显示出来,现在将转入主题

作者: C.R.CAN    时间: 2012-6-25 23:21
俺是新人,这厢有礼了!

作者: 晃晃    时间: 2012-9-7 23:52
不错不错,收藏了

作者: 晃晃    时间: 2012-9-10 23:27
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!

作者: 晃晃    时间: 2013-1-26 23:21
我是老实人,我来也!

作者: C.R.CAN    时间: 2013-3-13 23:21
路过、路过、快到鸟,列位请继续...ing

作者: tc    时间: 2013-3-17 23:21
我是老实人,我来也!

作者: fgdf#dfg    时间: 2013-4-15 08:28
感谢分享感谢分享感谢分享





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5