Major Softwares

  INDEX PAGE

  1. 簡介
  2. Java, Android和LibGDX好書推介
  3. Java, Android和LibGDX要學的知識
  4. 用Windows寫Java程式
  5. 用Eclipse寫Java程式
  6. 用Eclipse寫Android程式
  7. 用Eclipse寫LibGDX程式
  8. Standard Java Naming Conventions
  9. System.out.println()的用法和意思
  10. Where is main() method in Android?

LibGDX - Splash & Menu Screen

  1. LibGDX: World, Texture, Background, Camera, Viewport, Screen & OpenGL
  2. LibGDX: Texture, TextureRegion, SpriteBatch & Sprite
  3. UML(Unified Modeling Language): Class Diagram
  4. Type Casting, Upcasting & Downcasting
  5. @Override的用法和意思
  6. LibGDX: Scene2d & Graphical User Interface(GUI)
  7. LibGDX: Splash Screen
  8. LibGDX: Texture Packer
  9. LibGDX: BitmapFonts, JSON & Skin
  10. Android: R.java File
  11. Android: onClick事件的5種實現方式
  12. LibGDX: Game Menu Screen
  13. LibGDX: Advanced Game Menu Screen (using Abstract Screen)
  14. LibGDX: Disposable Interface
  15. Java & LibGDX: super keyword
  16. LibGDX: Advanced Game Menu Screen (using AssetManager)
  17. Java: Array, Arrays, List, ArrayList & LibGDX: Array
  18. LibGDX: File I/O (Preferences)
  19. LibGDX: Game Level Selection Screen
  20. LibGDX: Advanced Game Level Selection Screen (using AssetManager)

LibGDX - Tiled 2D Platform Game

  1. LibGDX: Lifecylce (Render() Method)
  2. LibGDX: Delta Time
  3. LibGDX: Animation & Spritesheet
  4. LibGDX: Keyboard, Mouse & Touch Screen Control
  5. Input Control (Polling VS Event Driven Input)
  6. LibGDX: Tiled (Background and Foreground)
  7. LibGDX: Jumping Action
  8. LibGDX: Tiled (Collision Detection)
  9. LibGDX: Tiled (Bullet Class)
  10. LibGDX: Audio (Sound & Music)
  11. LibGDX: Tiled (Scrollable Background with Camera & HUD)
  12. LibGDX: WorldController & WorldRenderer Class

LibGDX/Java - Card Game No.1 - Blackjack

  1. LibGDX: Install & Setup Android Studio IDE
  2. LibGDX: Use Android Studio to Run Java Hello World
  3. LibGDX: Use Android Studio to Run LibGDX Hello World
  4. Adobe Illustrator: Basic Components Part 1
  5. Adobe Illustrator: Basic Components Part 2
  6. Adobe Illustrator: BlackJack Table & Cards
  7. LibGDX: Blackjack Animation
  8. LibGDX: Interpolation
  9. Java: toString() Method
  10. Java: Blackjack Shuffle Methods
  11. LibGDX: Blackjack Shuffle Method
  12. Java: Blackjack Card Game

LibGDX - Others

  1. Making and Displaying App Icon
  2. LibGDX: Displaying Traditional and Simplified Chinese Characters
  3. LibGDX: Handling Different Screen Resolutions

Unity Game Engine & C#

  1. Visual Studio: C# Hello World
  2. Unity: C# Hello World
  3. Unity: Handling Different Screen Resolutions
  4. Unity: Life Cycle
  5. Unity: StartCoroutine, StopCoroutine, IEnumerator & Yield
  6. Unity: Splash Screen
  7. Unity: Fonts, Traditional and Simplified Chinese Characters
  8. Unity: GameObject, Class Object, new & Instantiate
  9. Unity: Start Screen with Glowing Animated Button
  10. Unity: C# Get & Set Modifier
  11. Unity: Delegates & Events
  12. Unity: File I/O, Read & Write Text File & PlayerPrefs
  13. Unity: Game Level Selection Screen
  14. Unity: Game Menu Screen & ScreenManager
  15. Unity: Encrypt and Decrypt Text File
  16. Unity: Options Menu Screen
  17. Unity: Convert Numbers Image to Custom Font

Unity - Card Game No.1 - Blackjack

  1. Unity: Blackjack Card Game - Part 1 (Full Game)
  2. Unity: Blackjack Card Game - Part 2
  3. Unity: Blackjack Card Game - Part 3
  4. Unity: Blackjack Card Game - Part 4
  5. Unity: Blackjack Card Game - Part 5
  6. Unity: Blackjack Card Game - Part 6
  7. Unity: Blackjack Card Game - Part 7

以下是預告-Coming soon!


Secret Weapon No.1

  1. Unity: Card Game No.2

Advanced Programming

  1. Unity: GPS Programming
  2. Unity: User Login System
  3. Unity: Augmented Reality (AR)

Secret Weapon No.2

  1. Unity: GPS & AR Application

第69節 - Unity: File I/O, Read & Write Text File & PlayerPrefs

我在第28節 - LibGDX: File I/O (Preferences)介紹過LibGDX的File I/O,File I/O就是用來把遊戲的資料儲存在電腦或手機上,每次開啟遊戲時程式就可以讀取遊戲資料,繼續遊戲。

這一節我會介紹Unity: File I/O & PlayerPrefs,主要介紹二種方法(Read & Write Text File和PlayerPrefs),當大家學會讀取和儲存數據,我會在下一節介紹如何讀取遊戲的狀況(Game State),再介紹如何製作Level Selection Screen,現在我會介紹以下方法:


Unity: C# Delegates & Events<

1: Read File Using Drag & Drop Text File

2: Read File Using Resources.Load() Method

3: Read File Using File.ReadAllLines() Method

4: Read File Using StreamReader Class and Stored in String

5: Read File Using StreamReader Class and Stored in List<T>

6: Read & Write File Using StreamReader & StreamWriter Class

7: Read & Write File Using PlayerPrefs (Example no.1 - Game HighScore)

8: Read & Write File Using PlayerPrefs (Example no.2 - English-Chinese Dictionary)

1.0) Read File Using Drag & Drop Text File

第一個例子運用Drag & Drop(拖放),把一個預設的Text File (.txt檔案)拖放到Unity的Inspector內:

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 建立Assets/Resources directory。

  3. 把C# MyScript1拖放到Main Camera內,再製作一個text file "myfile.txt",把它拖放到C# MyScript1內。

    注意: myfile.txt檔案可以用Notepad製作。

Unity: C# Delegates & Events<
  1. 大家可用Notepad製作以上三行的文字檔案(myfile.txt)。

Unity: C# Delegates & Events<
  1. 建立一個public TextAsset dataFile物件,這樣做就可以在Inspector內把text file "myfile.txt",拖放到C# MyScript1內。

  2. 把text file "myfile.txt"的三行資料讀取到Array dataLines[ ]內。

  3. 再把Array dataLines[ ]內的三行資料用分號"," 分析開,再儲存在Array dataPairs[ ][ ]內。

Unity: C# Delegates & Events<
  1. 執行結果。

2.0) Read File Using Resources.Load() Method

這一個例子運用C# Script Resources.Load()方法讀取檔案。

注意: 這一個例子會把文字檔案的所有內容一次過讀入一個TestAsset dataFile物件內,再作下一步處理,如果文字檔案的內容太大,就會影響執行效率,解決方法就是改用就四個例子,改用ScreamReader類別,我會詳細介紹。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 把C# MyScript2拖放到Main Camera內。

Unity: C# Delegates & Events<
  1. 建立一個TextAsset dataFile物件,用Resources.Load()方法讀取檔案

    注意: 文字檔案必須放在Asset/Resources Directory內。

  2. 把text file "myfile.txt"的三行資料讀取到Array dataLines[ ]內。

  3. 再把Array dataLines[ ]內的三行資料用分號"," 分析開,再儲存在Array dataPairs[ ][ ]內。

Unity: C# Delegates & Events<
  1. 執行結果。

3.0) Read File Using File.ReadAllLines() Method

這一個例子運用C# Script File.ReadAllLines()方法讀取檔案。

注意: 與第二個例子相似,這一個例子會把文字檔案的所有內容一次過讀入一個TestAsset dataFile物件內,再作下一步處理,如果文字檔案的內容太大,就會影響執行效率,解決方法就是改用就四個例子,改用ScreamReader類別,我會詳細介紹。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 把C# MyScript3拖放到Main Camera內。

Unity: C# Delegates & Events<
  1. 建立一個TextAsset dataFile物件,用Resources.Load()方法讀取檔案 。

  2. 把text file "myfile.txt"的三行資料讀取到Array dataLines[ ]內。

  3. 再把Array dataLines[ ]內的三行資料用分號"," 分析開,再儲存在Array dataPairs[ ][ ]內。

Unity: C# Delegates & Events<
  1. 執行結果。

4.0) Read File Using StreamReader Class and Stored in String

這一個方法運用C# Script StreamReader類別讀取資料,它的好處是每次只讀取一行資料,程式執行效率大大提升。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 把C# MyScript4拖放到Main Camera內。

Unity: C# Delegates & Events<
  1. 建立一個TextReader reader物件。

  2. 用reader.ReadLine()方法讀取檔案。

Unity: C# Delegates & Events<
  1. 執行結果。

5.0) Read File Using StreamReader Class and Store in List

這一個例子會把例子四運用C# Script StreamReader類別讀取資料,再把資料儲存在於List<T>內,它的好處是除了每次只讀取一行資料,程式執行效率大大提升外,資料儲存在List<T>內可以在程式執行時的任何時間內讀取資料。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 把C# MyScript5拖放到Main Camera內。

Unity: C# Delegates & Events<
  1. 建立一個StreamReader reader物件。

  2. 用reader.ReadLine()方法讀取檔案。

  3. 再把每行資料儲存在List<string>內。

  4. 大家也可以把每行資料儲存在Array內。

Unity: C# Delegates & Events<
  1. 執行結果。

6.0) Read & Write File Using StreamReader & StreamWriter Class

這一個方法會把例子五運用C# Script StreamReader類別讀取資料和運用StreamWriter把資料儲存在一個新的文字檔案內。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 把C# MyScript6拖放到Main Camera內。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 建立Assets/Resources directory。

  3. 把C# MyScript1拖放到Main Camera內,再製作一個text file "myfile2.txt",把它拖放到C# MyScript6內。

    注意: myfile2.txt檔案可以用Notepad製作。

Unity: C# Delegates & Events<
  1. 大家可用地Notepad製作以上空文字檔案(myfile.txt)。

Unity: C# Delegates & Events<
  1. 建立文字檔案儲存路徑。

  2. 建立要儲存的資料。

  3. 用StreamWriter類別儲存文字檔案。

  4. 用StreamReader類別讀取文字檔案。

Unity: C# Delegates & Events<
  1. 執行結果。

7.0) Read & Write File Using PlayerPrefs (Example no.1 - Game HighScore)

我在第28節 - LibGDX: File I/O (Preferences)介紹過LibGDX的Preferences,它與Unity的PlayerPrefs相似,它只可以用來儲存key-value pair的資料,則是只可以一對一,例如: HighScore=100等。

Unity: C# Delegates & Events<
  1. 選擇Main Camera。

  2. 把C# MyScript7拖放到Main Camera內。

Unity: C# Delegates & Events<
  1. 用PlayerPrefs.SetInt(Highscore, 100)方法建立須要儲存的資料。

    注意: 儲存的資料只可以是key-value pair,則是只可以一對一,例如: HighScore=100等。

  2. 用PlayerPrefe.Save())方法儲存資料。

  3. 用PlayerPrefs.GetInt(key, 0)方法方法讀取資料。

    注意: 在GetInt(key, 0) 的0是代表如果找不到key的值,就把指定的數值儲存到key上,即key=0。

Unity: C# Delegates & Events<
  1. 執行結果。

Unity: C# Delegates & Events<
  1. Data Type可以是PlayerPrefs.GetInt(key, value)、PlayerPrefs.GetString(key, value)或PlayerPrefs.GetFloat(key, value)。

以下是PlayerPrefs檔案在Windows 10儲存的位置。

Unity: C# Delegates & Events<
  1. 選擇Run。

  2. 輸入regedit。

  3. 按下OK。

Unity: C# Delegates & Events<
  1. 選擇在以上路徑。

  2. 就可以找到key = HighScore。

  3. Double Click就可以顯示Value = 100。

8.0) Read & Write File Using PlayerPrefs (Example no.2 - English-Chinese Dictionary)

以下會用PlayerPrefs舉一個英譯中字典(English-Chinese Dictionary)例子:

Unity: C# Delegates & Events<
  1. 建立一個Canvas。

  2. 設定Canvas的不同值。

  3. 選擇Main Camera,把Main Camera拖放到Render Camera內。

Unity: C# Delegates & Events<
  1. 在於Canvas內建立一個Text。

  2. 把C# MyScript8拖放到Text內。

Unity: C# Delegates & Events<
  1. 用PlayerPrefs.SetString(key, value)方法建立須要儲存的資料。

    注意: 儲存的資料只可以是key-value pair,則是只可以一對一,例如: HighScore=100等。

    用PlayerPrefe.Save())方法儲存資料。

  2. 用PlayerPrefs.GetString(key, "Zero")方法方法讀取資料。

    注意: 在GetString(key, "Zero") 的"Zero"是代表如果找不到key的值,就把指定的數值儲存到key上,即key=Zero。

  3. 顯示讀取資料。

Unity: C# Delegates & Events<
  1. Scene結果畫面。

Unity: C# Delegates & Events<
  1. Unity Console執行結果。

Unity: C# Delegates & Events<
  1. 遊戲執行結果。


Download above code and sample pictures here!