zoukankan      html  css  js  c++  java
  • Siki_Unity_1-4_C#编程(零基础)

    1-4 C#编程(零基础)

    任务1:第一章课程资料

    任务2:简介

    任务3:安装设置IDE工具

    Unity内置IDE:MonoDevelop

    推荐Visual Studio

    下载/安装 VS Community 2017 社区版就够用了
    Edit->Preference->External Tools->External Script Editor

    任务4:创建第一个C#代码

    任务5:场景的保存和脚本的保存

    脚本的文件名必须和脚本的类名保持一致,因为如果把一个脚本指定给一个GameObject,GameObject使用脚本的时候会根据脚本的文件名到脚本中查找相同名字的类进行加载

    任务6:单行注释和多行注释

    VS中单行注释快捷键:选中代码;ctrl+k ctrl+c

    任务7:日志输出

    print(); // 有限制,只能在组件里使用(继承MonoBehaviour类)(现在还没遇到非组件的情况)

    Debug.Log(); // 无限制,都可以使用
    Debug.LogWarning(); // 警告日志
    Debug.LogError(); // 错误日志

    日志在console窗口是可以过滤显示的

    任务8/9:声明变量

    不能以数字开头

    注意变量作用域

    任务10:小数和整数数据类型

    整数:

    sbyte = System.Sbyte  -- 8位有符号
    short = System.Int16 -- 16位有符号
    int = System.Int32 -- 32位有符号
    long = System.Int64 -- 64位有符号

    byte = System.Byte  -- 8位无符号
    ushort = System.UInt16 -- 16位无符号
    uint = System.UInt32 -- 32位无符号
    ulong = System.UInt64 -- 64位无符号

    浮点类型:

    float = System.Single -- 32位 (7位精度),注意赋值时的 f/F 后缀
    double = System.Double -- 64位(15/16位精度),常量的默认类型
    decimal = System.Decimal -- 128位(28位精度)

    任务11:其他数据类型

    bool/ char/ string

    任务12:算数操作符加减乘除

    + 可连接字符串

    任务13:比较运算符

    任务14:if语句

    任务15&16:数组声明和使用

    声明方式

    int[] hps = {100, 20, 90}; // 长度为3
    int[] hps = {};  // 长度为0
    int[] hps;  // 数组未创建,不可使用
    int[] hps = null;  // 数组未创建,但可以使用,比如print(hps);

    int[] hps = new int[10]; // int 默认值为0,string 默认值为nul

    int[] hps = new int[5] {34, 2, 324, 3, 34};
    // 初始化值个数必须与数组长度5相同,不能少也不能多

    任务17&18:for循环 & 用for循环遍历数组

    任务19:方法的定义和调用

    任务20:枚举类型

    在class外部可以进行枚举类型的定义:

    enum RoleType {
        Mag,
        Soldier,
        Wizard
    }

    枚举类型的使用:

    RoleType roleType = RoleType.Mag;
    roleType = RoleType.Wizard;

    任务21&22:方法中参数的作用 & 返回值

    任务23&24&25:类的创建、声明和构造 & 字段的使用 & 方法

    任务26:结束语

    任务27:第二章资料 -- 补充

    任务28:创建脚本和附加脚本(补充)

    任务29:脚本的基本结构(namespace)

    创建namespace命名空间

    在Player.cs中

    ...
    using MyGame;
    // 只有在Player中引入了该命名空间,GameData才能在Player中使用
    
    public class Player : MonoBehaviour {
        void Start() {
            GameData data;
        }
    }
    
    namespace MyGame {
        class GameData {
            ...
        }
    }

    MonoBehaviour是在UnityEngine下的一个类

    任务30:变量的定义

    若有一个public变量,在脚本中初始化值为0,在Unity中Inspector里也会被初始为0
    当在脚本中修改代码初始化为10后,Unity中的值不会自动改变,还是0
    变量的值优先选择Unity中Inspector面板的值,所以运行的时候变量值依然为0

    如果没有给定一个变量public/private修饰符,默认为private

    任务31&32:逻辑运算符

    任务33:if变形和枚举类型的使用

    任务34:switch语句

    任务35:for、while和dowhile循环

    若要删除一个GameObject的所有子物体:

    Transform[] children = transform.GetComponentsInChildren<Transform>();
    // 返回的子物体中包含了当前物体
    for(int i = 0; i < children.Length; i++) {
      if (children[i] != transform) {  // 如果该物体不是当前物体
        GameObject.Destroy(children[i].gameObject);
      }
    }

    任务36:foreach的遍历

    foreach用来遍历集合和数组

    任务35中的例子:

    foreach(Transform t in children) {
        if (t != transform) {
            Destroy(t.gameObject);
        }
    }

    任务37&38:组件的获取 & 禁用和激活

    组件的获取:

    访问自身的组件:
      GetComponent<comp_name>();

    transform; // transform组件可以直接获得

    Collider[] colliders = GetComponents<Collider>();  // 获得所有的Collider组件

    拖拽赋值:

    public comp_name xxx;
    在unity中拖拽赋值;

    获取子物体的组件:

    GetComponentsInChildren<xxx>();  // 所有孩子的所有该组件

    GetComponentInChildren<xxx>();  // 返回第一个得到的该组件

    组件内属性的获取/修改:

    Rigidbody rgd = player.GetComponent<Rigidbody>();
    rgd.mass = 100;

    组件的禁用和激活

    被禁用了的组件也是可以被获取的

    1. 获取组件

    BoxCollider collider = GetComponent<BoxCollider>();

    2. 禁用

    collider.enabled = false;

    注:当一个脚本组件被禁用后,里面的方法依然可以被别的脚本手动调用
      但是那些Unity自动调用的方法就不会被自动调用了,比如Update()/ OnTriggerEnter()

    任务39:获取游戏物体的四种方式

    1. 拖拽方式 (也是获取组件的方式之一)

    2. transform.Find("Child_name") -- 只能用于获取后代物体 -- 推荐使用
      transform.Find("Child_name/Grandchild_name"); // 用/隔开
      如果给的路径不存在的话,会返回Null

    3. GameObject.Find("Object_name");  // 查找全局 -- 不推荐使用(耗费性能)

    4. GameObject.FindWithTag("Tag_name");  // 查找全局 -- 推荐

     

  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/FudgeBear/p/8025763.html
Copyright © 2011-2022 走看看