2019-04-11 19:45:19 yffgamestart 阅读数 564

如果想直接使用enabled来禁用脚本或者组件,必须继承至MonoBehaviour

 camera.enabled = false;

C#:
GetComponent().enabled = true;

如果不是继承MonoBehaviour的做法:
(gameObject.GetComponent(“MyScript”) as MonoBehaviour).enabled = false;

**一个致力于unity游戏,模型,等资源的淘宝店铺,里面有unity3d成品游戏,场景资源,模型资源等,一切资源都特别优惠,店铺链接:【unity3D素材包/unity3d中世纪村庄包Medieval Village Pack】https://m.tb.cn/h.eaENjDH?sm=2b946c 点击链接,再选择浏览器咑閞;或復·制这段描述¥kIKkbAV3c3C¥后到?淘♂寳♀?
店铺链接:
【峰回路转设计之巅】,復·制这段描述¥Rdl0bARAdpZ¥后咑閞?手机淘宝?或者用浏览器咑閞https://m.tb.cn/h.eZyY9BV?sm=f141d2查看

2014-04-08 03:37:19 huang9012 阅读数 14112

如果想直接使用enabled来禁用脚本或者组件,必须继承至MonoBehaviour

1 camera.enabled = false;

JS脚本里:

1 GetComponent(MyScript).enabled = false;

C#:

1 GetComponent<MyScript>().enabled = true;

如果不是继承MonoBehaviour的做法:

1 (gameObject.GetComponent("MyScript"as MonoBehaviour).enabled = false;
2012-11-29 23:27:40 chinalofthjh 阅读数 137
【专业移动app开发,定制开发.主力提供iOS,Andorid系统软件.快速开发,质量保证.拥有专业界面设计人员,界面分析人员.给

您一个不一样的 3d app.web 3d、webGL、flash 3d、app 三维动画 unity3d app unity3d 公司宣传 虚拟现实 产品360度展

示 app 手机应用开发:北京贝武易科技公司 热线电话:18600160534】
有疑问请联系我QQ:1539988257
贝武易-APP 3D技术联盟机构,提供研究、交流和培训机会,欢迎加入。
贝武易-unity3d app技术联盟群:49771294


#pragma strict

function Start () {

}

function Update () {
if(Input.GetKey(KeyCode.Escape)){
Application.Quit();
}
}

function OnGUI ()
{// Make a background box
GUI.Box (Rect (10,10,100,90), "Loader Menu");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
Application.LoadLevel (1);
}

// Make the second button.
if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
Application.LoadLevel (2);
}
}
2015-05-16 11:14:44 a237653639 阅读数 6316


本文章由攀大小熊猫编写,转载请注明出处。

文章链接:http://blog.csdn.net/a237653639/article/details/45766101

作者: 攀大小熊猫    邮箱: 237653639@qq.com  


先总结,再证明。

总结:

禁用脚本的目的就是为了让该脚本“不执行”,而启用的目的就是为了让该脚本”执行“。以上是我原来的想法,其实这样的理解有偏差。准确地说,即使脚本为禁用状态,别的脚本也能够成功地调用 被禁用的脚本的成员(后面将给以证明


那么,您可能会发问,那我禁用脚本有什么意义呢?

其实意义就是,脚本为启用状态时,unity会自动调用该脚本的类继承自MonoBehaviour的方法,反之,脚本为禁用状态时,unity就不会调用这些方法。但绝不影响其他脚本对该脚本内成员的调用


证明:

   即使脚本为禁用状态,别的脚本也能够成功地调用 被禁用的脚本的成员

步骤1,先创建一个cube,然后在其上挂一个Test脚本。然后在MainCamera上挂一个Test2脚本。

   我们将禁用Test脚本,Test2将调用Test内的成员。

  如下图:

cube:

camera:


Test脚本的内容为:

Test2的内容为:

注意,我们已经把Test脚本设为了禁用。

现在,我们看到了运行结果,如下图。cube.GetComponent<Test>().Print()这句话调用成功,但是Test中的继承自MonoBehaviour的Update方法并没有被调用。


现在解释脚本前面的复选框消失的原因:

首先,复选框消失的原因我先复制粘贴下雨松大神的解释:

凡事脚本没有勾选项的,都是因为你的脚本没有Start方法。。如果你想让勾选框显示出来,

把如下方法加入即可,不信你可以试试。嘿嘿。有人测试了一下, 

加上 Update LateUpdate FixedUpdate OnGUI 也都可以让勾选框出来。。。但是Awake不行。。

通过雨松大神的解释,以下是我进一步的想法。

通过上面的总结,我们已经知道了:脚本为启用状态时,

unity会自动调用该脚本的类继承自MonoBehaviour的方法,

反之,脚本为禁用状态时,unity就不会调用这些方法。

我的想法:

如果你的脚本里面没有继承自MonoBehaviour的方法,意思就是就不需要unity来调用了(根本没有,从何调用)。

这样的话,你的脚本不管是禁用或是启用就都跟Unity没有半毛钱的关系了,

所以Unity才会不给你显示复选框,因为没有必要嘛对吧。而一旦有比如Start这类继承(其实是覆写)了

MonoBehaviour的方法,这时就该Unity出马了!所以复选框就会重新显示出来,

由你来控制是否允许Unity来调用Start等方法。那么Awake呢? 

们查阅Unity的官方文档上可以知道(Awake is called once, just like the constructor.),

如果重新启用一个脚本,那么该脚本内的Awake方法不会被调用,而Start等方法会重新起作用(调用)。

而Awake只在其脚本附加的对象一开始被创建时调用一次(不管该脚本是启用还是禁用

都会被调用一次),以后不再调用。所以”Awake不行“(不能使复选框重新出现),哈哈。

the end^_^

2012-06-14 13:39:21 wangxfvc 阅读数 4254

Unity3D管理多相机很是繁琐,"Camera Preview"着实解决了相机的切换,然而窗口小,比例不能调整,并且在多个相机中禁用启用设置相机视角也很不方便,所以也不是很实用。

Security Camera就简化了这个过程。以下是Security Camera脚本的使用说明:

1、将SecurityCameraEditor放在Editor中,其脚本为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor(typeof(SecurityCamera))]
public class SecurityCameraEditor : Editor
{
    SecurityCamera _target = null;
     
    void OnEnable(){
        if(_target == null && !Application.isPlaying){
            _target = (SecurityCamera)target;
            _target.camera.depth = 999999;
        }
    }
     
    void OnDisable(){
        if (!Application.isPlaying && _target != null) {
            _target.camera.depth = -999999;
        }
    }
}

 2、将SecurityCamera脚本和CameraSwap脚本放在Camera Script文件夹下,其中SecurityCamera脚本要加在每个相机上(主相机MainCamera的Tag为MainCamera,其他相机如Camera1,Camera2,Camera3),CameraSwap脚本加在GameObject(或者其他的物体上)。

SecurityCamera脚本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
using UnityEngine;
using System.Collections.Generic;
 
public class SecurityCamera : MonoBehaviour{
    public static Dictionary<string, GameObject> Cameras = new Dictionary<string, GameObject>();
    public static GameObject CurrentCamera;
    static int conflictCount = 0;
     
    void Start(){
        if (Cameras.ContainsKey(name)) {
            Debug.LogWarning("SecurityCamera: Warning! All cameras used with SecurityCamera should have unique names. Conflict has been automatically renamed."); 
            name = name + "_" + ++conflictCount;
        }
         
        if (tag == "MainCamera" && name != "Main Camera") {
            Debug.LogWarning("SecurityCamera: Warning! A camera was found with a tag of 'MainCamera' but wasn't named 'Main Camera' this might be the result duplicating the default Main Camera. This camera's tag has been set to 'Untagged' to ensure proper operation of Unity's Camera.main.");
            tag = "Untagged";
        }
         
        Cameras.Add(name,gameObject);
        if (Camera.main.gameObject != gameObject) {
            gameObject.active = false;
        }else{
            CurrentCamera = Camera.main.gameObject;
        }
    }
     
    public static void ChangeCamera(string newCamera){
        CurrentCamera.active = false;
        Cameras[newCamera].active = true;
        CurrentCamera = Cameras[newCamera];
        CurrentCamera.camera.depth=999999;
    }
}

CameraSwap脚本为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using System.Collections;
 
public class CameraSwap : MonoBehaviour
{
    void Update ()
    {
        //pressing 0, 1, 2, and 3 will swap before the cameras named "Main Camera",
        //"Camera 1", "Camera 2", and "Camera 3" which have Security Camera added:
        if (Input.GetKeyDown(KeyCode.Alpha0)) {
            SecurityCamera.ChangeCamera("Main Camera");
        }
        if (Input.GetKeyDown(KeyCode.Alpha1)) {
            SecurityCamera.ChangeCamera("Camera1");
        }
        if (Input.GetKeyDown(KeyCode.Alpha2)) {
            SecurityCamera.ChangeCamera("Camera2");
        }
        if (Input.GetKeyDown(KeyCode.Alpha3)) {
            SecurityCamera.ChangeCamera("Camera3");
        }
    }
}

其文件组织格式如下:

参考网站:http://pixelplacement.com/2011/02/14/security-camera/

Unity脚本生命周期

阅读数 1702

没有更多推荐了,返回首页