cSharpInUnity

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//枚举类型
enum RoleType
{
	Mag,
    Soldier,
    Wizard
}
public class Cube1 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        unity中的c#:




        1.注释
            单行、多行注释
                多行:中间的* 有没有都无所谓,是为了保持整齐




        控制台输出的几种方式
        print(1);
        print("2222");
        debug.log("2649");

        debug.logwarning("25448");

        debug.logerror("1156456131");
        2.控制台输出
            几种控制台输出方式
                print()
                    可以输出数字 或 字符串
                    只有当类继承 monobehaviour类 才可以使用
                    输出普通消息
                debug.log("2649");
        只能 输出 字符串
                    和print()输出的效果没有不同
                    只是在任何位置都可以输出,不需要继承类
                    输出普通消息
                debug.logwarning("25448");
        输出警告消息
                debug.logerror("1156456131");
        输出错误信息



变量声明
        int hp = 100;
        int hp;
        hp = 100;
        print(hp);



        变量的类型
        float hp2 = 1.2f;
        bool isdie = true;
        char c = 'c';
        string name = "绝地战士";



        运算符
        int a = 90; int b = 10;
        int res1 = a + b;
        int res2 = a - b;
        int res3 = a * b;
        int res4 = a / b;
        float res5 = a + 3.2f;
        string str = "123" + "456";
        hp += 10;
        hp++;
        hp--;



        比较运算符
        int hp = 100;
        bool res = 7 > 5;
        print(hp > 100);



        if语句
        int hp = 0;
        if (hp <= 0)
        {
            print("播放死亡动画");
        }
        else
        {
            print("播放行走动画");
        }



        数组
        int[] hps = { 100, 20, 50 };
        print(hps[1]);
        int[] hps2 = { };
        hps[1] = 50;

        int[] hps = new int[10];
        print(hps[0]);

        int[] hps = null;
        int[] hps2 = new int[5] { 1, 2, 3, 4, 5 };


        for循环
        for (int i = 0; i < 10; i++)
        {
            print("创建了一个敌人");
        }
        for (int i = 0; i < hps2.Length; i++)
        {
            print(hps2[i]);
        }



        方法的定义和调用
        Test();
        CreateEnemy(new Vector3(3, 3, 4));


        枚举类型
        RoleType rt = RoleType.Mag;
        rt = RoleType.Soldier;Enemy enemy1 = new Enemy();
        enemy1.name = "Mary";

        print(enemy1.name);
        print(enemy1.hp);
        enemy1.Move();
        enemy1.Attack();
    }

    // Update is called once per frame
    void Update()
    {
        //print(2);
        
    }



    //方法的定义和调用
    void Test()
    {
        print("Test方法被调用了");
    }
    void CreateEnemy(Vector3 pos)
    {
        print("创建敌人");
        print("设置敌人位置"+pos);
        print("设置敌人初始属性");
    }
    
}


//类
class Enemy
{
    public string name;
    public int hp;
    public void Move()
    {
        Debug.Log("正在移动");
    }
    public void Attack()
    {
        Debug.Log("正在攻击");
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyGame;


//命名空间的应用
//引入命名空间 才可以使用 命名空间中的类
//通过 using 命名空间名; 引入


public class Cube2 : MonoBehaviour
{



    // Start is called before the first frame update
    public void Start()
    {
        GameDate data;
        print("cube2里的start方法被调用了");
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ShowMessage()
    {
        print("I'm here.");
    }
}


定义一个命名空间
namespace 命名空间名
{
    里面可以写 类
}

namespace MyGame
{
    class GameDate
    {

    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube3: MonoBehaviour
{

    /*
     * 数学运算符
     * + - * /
     * 赋值运算符
     * += -= *= /=
     * 比较运算符
     * > >= < <= == !=
     * 逻辑运算符(bool)
     *  && || !
     */


    enum HeroType
    {
        Soldier,
        Master,
        Assassin,
        Tank,
        Shooter
    }

    // Start is called before the first frame update
    void Start()
    {
        //if变形 和 枚举类型的使用
        HeroType ht = HeroType.Assassin;
    
        switch (ht)
        {
            case HeroType.Assassin:
                print("a");
                break;
            case HeroType.Master:
                print("m");
                break;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Learn04 : MonoBehaviour
{
    第三种获取组件的方式——直接拖拽(当要获取的组件也位于这个物体时)
    public Cube2 cube2;
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        Transform[] children = transform.GetComponentsInChildren<Transform>();
        for (int i = 0; i < children.Length; i++)
        {
            if (children[i] != transform)
            {
                GameObject.Destroy(children[i].gameObject);
            }
        }
        foreach (Transform t in children)
        {
            if (t != transform)
            {
                Destroy(t.gameObject);
            }
        }


        组件的获取
        1.GetComponent
        Transform t = GetComponent<Transform>();//获//取自身的组件(不管组件是否被禁用)
        注:transform本身不需要这样获取 它是一个内置的组件 可以直接写 transform 这里只是实例
        2.GetComponents
        Collider[] colliders = GetComponents<Collider>();
        foreach (Collider c in colliders)
        {
            print(c);
        }
        BoxCollider[] boxCollider = GetComponents<BoxCollider>();
        4.GetComponentsInChildren <> 从孩子、孩子的孩子、以及自身获取组件
        5.GetComponentInChildren <> 从孩子、孩子的孩子、以及自身获取组件,只返回第一个得到的组件
        6.如何获取另一个游戏物体身上的组件
        print(player.GetComponent<Rigidbody>());



        组件的禁用和激活
        刚体不能被禁用
        首先获取组件
        BoxCollider boxCollider = player.GetComponent<BoxCollider>();
        boxCollider.enabled = false;

        cube2.Start();
        cube2.ShowMessage();


        如何访问组件的属性
        Rigidbody rgb = player.GetComponent<Rigidbody>();
        rgb.mass = 100;




        获取游戏物体的四种方式

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Learn05 : MonoBehaviour
{
    public GameObject cameraMain;
    public Camera mainCamera;
    // Start is called before the first frame update
    void Start()
    {
        获取游戏物体(推荐1.2.41.拖拽
        例如:将Main Camera拖拽到cameraMain 和 将Main Camera拖拽到mainCamera上(自动查找Main Camera游戏物体上的Camera组件)
        2.transform.Find查找子物体,返回Transform组件
        transform.Find("GameObject");
        transform.Find("GameObject/GameObject");
        3.全局查找,返回第一个,返回GameObject
        GameObject.Find("GameObject");
        4.根据标签进行查找,返回GameObject
        GameObject.FindWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值