最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#入門之?dāng)?shù)組與列表List的創(chuàng)建與使用詳解

 更新時(shí)間:2026年05月20日 09:18:02   作者:星河耀銀海  
本文介紹了Unity中數(shù)組和列表(List)的核心概念與使用方法,主要內(nèi)容包括基本概念,創(chuàng)建方式,核心操作等,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下

本章學(xué)習(xí)目標(biāo):深入理解數(shù)組與列表List的創(chuàng)建與使用的核心概念與實(shí)踐方法,掌握關(guān)鍵技術(shù)要點(diǎn),了解實(shí)際應(yīng)用場景與最佳實(shí)踐。本文屬于《Unity工程師成長之路教程》Unity C#入門篇(第二篇)。

在上一章,我們學(xué)習(xí)了"Unity C#入門:循環(huán)語句(for/while)的實(shí)戰(zhàn)應(yīng)用"。本章,我們將深入探討數(shù)組與列表List的創(chuàng)建與使用,這是Unity游戲開發(fā)中非常重要的一環(huán)。

一、核心概念與背景

1.1 什么是數(shù)組與列表List的創(chuàng)建與使用

基本定義

數(shù)組與列表List的創(chuàng)建與使用是Unity游戲開發(fā)中的核心知識點(diǎn)之一。掌握這項(xiàng)技能對于提升游戲開發(fā)效率和項(xiàng)目質(zhì)量至關(guān)重要。

// Unity C# 示例代碼
using UnityEngine;

public class ExampleScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Hello, Unity!");
    }
    
    // Update is called once per frame
    void Update()
    {
        // 每幀執(zhí)行的邏輯
    }
}

1.2 為什么數(shù)組與列表List的創(chuàng)建與使用如此重要

重要性分析

在實(shí)際游戲開發(fā)過程中,數(shù)組與列表List的創(chuàng)建與使用的重要性體現(xiàn)在以下幾個(gè)方面:

  1. 開發(fā)效率提升:掌握這項(xiàng)技能可以顯著減少開發(fā)時(shí)間
  2. 游戲性能保障:幫助開發(fā)者創(chuàng)建更流暢、更高效的游戲
  3. 問題解決能力:遇到相關(guān)問題時(shí)能夠快速定位和解決
  4. 職業(yè)發(fā)展助力:這是從新手到高級Unity工程師的必經(jīng)之路

1.3 應(yīng)用場景

典型應(yīng)用場景

場景類型具體應(yīng)用技術(shù)要點(diǎn)
游戲開發(fā)角色控制、游戲邏輯組件設(shè)計(jì)、腳本編寫
UI系統(tǒng)界面交互、數(shù)據(jù)展示Canvas布局、事件系統(tǒng)
物理模擬碰撞檢測、剛體運(yùn)動物理組件、射線檢測
資源管理資源加載、內(nèi)存優(yōu)化AssetBundle、對象池

二、技術(shù)原理詳解

2.1 核心原理

Unity架構(gòu)概述

Unity的核心架構(gòu)包含以下幾個(gè)關(guān)鍵組件:

┌─────────────────────────────────────────────────────────┐
│                    Unity核心架構(gòu)                         │
├─────────────────────────────────────────────────────────┤
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
│  │  游戲?qū)ο?  │  │  組件系統(tǒng)   │  │  場景管理   │     │
│  │ (GameObject)│  │ (Component) │  │  (Scene)    │     │
│  └─────────────┘  └─────────────┘  └─────────────┘     │
│         ↑                                    ↓          │
│  ┌─────────────────────────────────────────────────┐   │
│  │              腳本系統(tǒng) (MonoBehaviour)            │   │
│  └─────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────┘

2.2 實(shí)現(xiàn)方法

using UnityEngine;

/// <summary>
/// Unity組件示例類
/// </summary>
public class UnityDemo : MonoBehaviour
{
    [Header("基本設(shè)置")]
    [SerializeField] private string objectName = "Unity對象";
    [SerializeField] private float moveSpeed = 5f;
    
    private Transform cachedTransform;
    
    /// <summary>
    /// 初始化方法
    /// </summary>
    private void Awake()
    {
        cachedTransform = transform;
        Debug.Log($"{objectName} 已初始化");
    }
    
    /// <summary>
    /// 開始方法
    /// </summary>
    private void Start()
    {
        // 初始化邏輯
    }
    
    /// <summary>
    /// 更新方法
    /// </summary>
    private void Update()
    {
        // 移動邏輯
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        
        Vector3 movement = new Vector3(horizontal, 0, vertical);
        cachedTransform.Translate(movement * moveSpeed * Time.deltaTime);
    }
}

2.3 關(guān)鍵技術(shù)點(diǎn)

技術(shù)點(diǎn)說明重要性
組件化設(shè)計(jì)一切皆組件,靈活組合?????
生命周期函數(shù)Awake/Start/Update等?????
序列化字段Inspector面板顯示????
預(yù)制體Prefab資源復(fù)用與實(shí)例化?????

三、實(shí)踐應(yīng)用

3.1 環(huán)境準(zhǔn)備

① 安裝Unity Hub

步驟1: 訪問Unity官網(wǎng)下載Unity Hub

步驟2: 安裝Unity Hub并登錄賬號

步驟3: 在Unity Hub中安裝Unity編輯器

步驟4: 創(chuàng)建新項(xiàng)目或打開現(xiàn)有項(xiàng)目

② 創(chuàng)建第一個(gè)腳本

// 右鍵 Assets 文件夾
// Create -> C# Script
// 命名為 MyFirstScript

using UnityEngine;

public class MyFirstScript : MonoBehaviour
{
    // 在Inspector面板中顯示的變量
    public int health = 100;
    public float speed = 5.0f;
    public string playerName = "Player1";
    
    void Start()
    {
        Debug.Log($"玩家 {playerName} 已創(chuàng)建,生命值: {health}");
    }
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("空格鍵被按下");
        }
    }
}

3.2 基礎(chǔ)示例

示例一:游戲?qū)ο罂刂?/strong>

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("移動設(shè)置")]
    public float moveSpeed = 5f;
    public float rotateSpeed = 100f;
    
    private Rigidbody rb;
    
    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }
    
    private void Update()
    {
        // 獲取輸入
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        
        // 移動
        Vector3 movement = new Vector3(horizontal, 0, vertical);
        transform.Translate(movement * moveSpeed * Time.deltaTime);
        
        // 旋轉(zhuǎn)
        if (Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(0, -rotateSpeed * Time.deltaTime, 0);
        }
        if (Input.GetKey(KeyCode.E))
        {
            transform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
        }
    }
}

示例二:UI交互

using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    [Header("UI組件")]
    public Text scoreText;
    public Button startButton;
    public Slider healthSlider;
    
    private int score = 0;
    
    private void Start()
    {
        // 綁定按鈕事件
        startButton.onClick.AddListener(OnStartButtonClicked);
        
        // 初始化UI
        UpdateScoreDisplay();
        healthSlider.value = 100;
    }
    
    public void AddScore(int points)
    {
        score += points;
        UpdateScoreDisplay();
    }
    
    private void UpdateScoreDisplay()
    {
        scoreText.text = $"分?jǐn)?shù): {score}";
    }
    
    private void OnStartButtonClicked()
    {
        Debug.Log("游戲開始!");
        // 開始游戲邏輯
    }
}

3.3 進(jìn)階示例

using UnityEngine;
using System;

/// <summary>
/// 單例模式管理器示例
/// </summary>
public class GameManager : MonoBehaviour
{
    // 單例實(shí)例
    public static GameManager Instance { get; private set; }
    
    [Header("游戲設(shè)置")]
    [SerializeField] private int maxLives = 3;
    [SerializeField] private float gameTime = 0f;
    
    // 事件
    public event Action<int> OnLivesChanged;
    public event Action<float> OnTimeChanged;
    
    private int currentLives;
    private bool isGameRunning;
    
    private void Awake()
    {
        // 單例初始化
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        DontDestroyOnLoad(gameObject);
        
        // 初始化游戲狀態(tài)
        currentLives = maxLives;
    }
    
    private void Update()
    {
        if (isGameRunning)
        {
            gameTime += Time.deltaTime;
            OnTimeChanged?.Invoke(gameTime);
        }
    }
    
    public void StartGame()
    {
        isGameRunning = true;
        gameTime = 0f;
        currentLives = maxLives;
        OnLivesChanged?.Invoke(currentLives);
    }
    
    public void LoseLife()
    {
        currentLives--;
        OnLivesChanged?.Invoke(currentLives);
        
        if (currentLives <= 0)
        {
            GameOver();
        }
    }
    
    private void GameOver()
    {
        isGameRunning = false;
        Debug.Log("游戲結(jié)束!");
    }
}

四、常見問題與解決方案

4.1 環(huán)境配置問題

問題一:腳本無法掛載到游戲?qū)ο?/strong>

現(xiàn)象

Can't add script component 'ExampleScript' because the script class cannot be found.

解決方案

1. 確保腳本類名與文件名完全一致

2. 確保腳本繼承自MonoBehaviour

3. 檢查腳本是否有編譯錯(cuò)誤

4. 嘗試在Unity中右鍵 -> Reimport All

問題二:Inspector面板變量不顯示

現(xiàn)象:public變量在Inspector中看不到

解決方案

// 方案1: 使用public(不推薦)
public int value;

// 方案2: 使用SerializeField(推薦)
[SerializeField] private int value;

// 方案3: 添加Header屬性
[Header("設(shè)置")]
[SerializeField] private int value;

// 方案4: 添加Range屬性
[Range(0, 100)]
[SerializeField] private int value;

4.2 運(yùn)行時(shí)問題

問題三:空引用異常

現(xiàn)象

NullReferenceException: Object reference not set to an instance of an object

解決方案

// 錯(cuò)誤寫法
private void Start()
{
    rb.AddForce(Vector3.up);  // rb可能為null
}

// 正確寫法
private Rigidbody rb;

private void Awake()
{
    rb = GetComponent<Rigidbody>();
}

private void Start()
{
    if (rb != null)
    {
        rb.AddForce(Vector3.up);
    }
    else
    {
        Debug.LogError("Rigidbody組件未找到!");
    }
}

問題四:性能問題

現(xiàn)象:游戲運(yùn)行卡頓

解決方案

// 優(yōu)化1: 緩存組件引用
private Transform cachedTransform;

private void Awake()
{
    cachedTransform = transform;  // 緩存Transform
}

// 優(yōu)化2: 避免在Update中使用Find
private GameObject target;

private void Start()
{
    target = GameObject.Find("Target");  // 只在Start中查找一次
}

// 優(yōu)化3: 使用對象池
private List<GameObject> objectPool = new List<GameObject>();

public GameObject GetObject()
{
    foreach (var obj in objectPool)
    {
        if (!obj.activeInHierarchy)
        {
            obj.SetActive(true);
            return obj;
        }
    }
    // 創(chuàng)建新對象...
    return null;
}

五、最佳實(shí)踐

5.1 代碼規(guī)范

推薦做法

// 1. 使用有意義的變量名
public float playerMoveSpeed = 5f;  // ? 好
public float s = 5f;  // ? 不好

// 2. 添加注釋和文檔
/// <summary>
/// 玩家控制器,處理玩家輸入和移動
/// </summary>
public class PlayerController : MonoBehaviour
{
    /// <summary>
    /// 玩家移動速度
    /// </summary>
    [Tooltip("玩家移動速度,單位:米/秒")]
    [SerializeField] private float moveSpeed = 5f;
}

// 3. 使用SerializeField而非public
[SerializeField] private int health;  // ? 推薦
public int health;  // ? 不推薦

// 4. 使用事件解耦
public event Action OnPlayerDeath;

private void Die()
{
    OnPlayerDeath?.Invoke();
}

5.2 性能優(yōu)化技巧

技巧說明效果
緩存組件引用避免重復(fù)GetComponent提升10倍速度
對象池復(fù)用游戲?qū)ο?/td>減少GC壓力
批量處理合并相同操作減少Draw Call
LOD系統(tǒng)根據(jù)距離降低細(xì)節(jié)提升渲染效率

5.3 安全注意事項(xiàng)

安全檢查清單

  • 所有組件引用在使用前檢查null
  • 使用SerializeField保護(hù)變量
  • 避免在Update中分配內(nèi)存
  • 合理使用對象池
  • 注意資源釋放和內(nèi)存管理

六、本章小結(jié)

6.1 核心要點(diǎn)回顧

要點(diǎn)一:理解數(shù)組與列表List的創(chuàng)建與使用的核心概念和原理

要點(diǎn)二:掌握基本的實(shí)現(xiàn)方法和代碼示例

要點(diǎn)三:了解常見問題及解決方案

要點(diǎn)四:學(xué)會最佳實(shí)踐和性能優(yōu)化技巧

6.2 實(shí)踐建議

學(xué)習(xí)階段建議內(nèi)容時(shí)間安排
入門完成所有基礎(chǔ)示例1-2周
進(jìn)階獨(dú)立完成一個(gè)小游戲2-4周
高級優(yōu)化性能,處理復(fù)雜場景1-2月

到此這篇關(guān)于C#入門之?dāng)?shù)組與列表List的創(chuàng)建與使用詳解的文章就介紹到這了,更多相關(guān)C#數(shù)組與列表List內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼

    C# Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼

    這篇文章主要介紹了C# Winfrom實(shí)現(xiàn)Skyline畫直線功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼

    c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼

    這篇文章主要介紹了c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼,有需要的朋友可以參考一下
    2013-12-12
  • C#中IEnumerable、ICollection、IList、List之間的區(qū)別

    C#中IEnumerable、ICollection、IList、List之間的區(qū)別

    IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實(shí)現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處。對C# IEnumerable、ICollection、IList、List相關(guān)知識,感興趣的朋友一起看看吧
    2021-07-07
  • C#中dynamic的使用方法及應(yīng)用場景

    C#中dynamic的使用方法及應(yīng)用場景

    在 C# 編程中,dynamic 類型是一個(gè)非常特殊的類型,它在編譯時(shí)并不會進(jìn)行類型檢查,而是在運(yùn)行時(shí)才進(jìn)行類型解析,本文將詳細(xì)講解 dynamic 的使用方法、優(yōu)缺點(diǎn)以及一些實(shí)際應(yīng)用場景,需要的朋友可以參考下
    2024-08-08
  • .Net Winform開發(fā)筆記(一)

    .Net Winform開發(fā)筆記(一)

    理解“Windows 窗體應(yīng)用程序”項(xiàng)目中Program.cs文件中的main方法與傳統(tǒng)C++Console控制臺程序中的main方法的區(qū)別等等,感興趣的朋友可以了解下
    2013-01-01
  • C#網(wǎng)頁信息采集方法匯總

    C#網(wǎng)頁信息采集方法匯總

    這篇文章主要介紹了C#網(wǎng)頁信息采集方法,實(shí)例匯總了三種常用的方法,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • C#實(shí)現(xiàn)將PowerPoint演示文稿高效轉(zhuǎn)換為PDF格式的兩種方法詳解

    C#實(shí)現(xiàn)將PowerPoint演示文稿高效轉(zhuǎn)換為PDF格式的兩種方法詳解

    在企業(yè)級應(yīng)用、自動化辦公或文檔管理系統(tǒng)中,常常需要將 PowerPoint(.ppt 或 .pptx)文件批量轉(zhuǎn)換為 PDF 格式,本文將詳細(xì)介紹兩種主流方法,有需要的小伙伴可以了解下
    2026-01-01
  • 輕松學(xué)習(xí)C#的String類

    輕松學(xué)習(xí)C#的String類

    輕松學(xué)習(xí)C#的String類,小編也是第一次接觸C#的String類,感興趣的小伙伴們可以參考一下,大家一起學(xué)習(xí)
    2015-11-11
  • C#開啟線程的四種示例

    C#開啟線程的四種示例

    這篇文章主要介紹了C#開啟線程的四種方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-10-10
  • C# out關(guān)鍵詞的應(yīng)用實(shí)例

    C# out關(guān)鍵詞的應(yīng)用實(shí)例

    下面小編就為大家分享一篇C# out關(guān)鍵詞的應(yīng)用實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12

最新評論

丰镇市| 商洛市| 灵寿县| 西安市| 琼结县| 丽江市| 汤原县| 都昌县| 楚雄市| 萨迦县| 云安县| 兴国县| 河北区| 沾化县| 阿图什市| 土默特左旗| 昭觉县| 太仓市| 阳东县| 新巴尔虎左旗| 资阳市| 南漳县| 泾源县| 宁陵县| 宁津县| 嘉禾县| 伊宁市| 新龙县| 嘉定区| 沐川县| 雷州市| 治多县| 巍山| 新蔡县| 商河县| 无为县| 拜泉县| 民权县| 万载县| 新乐市| 金平|