公告版位
需要找什麼嗎? 左邊分類或許可以幫助你...

目前分類:[遊戲設計] Unity (100)

瀏覽方式: 標題列表 簡短摘要
記得還要在此程式碼外部加入 "Audio Source" 元件喔!!

using UnityEngine;
using System.Collections;
 
public class Main : MonoBehaviour {
 
    bool isRecord = true;
 
    void OnGUI(){

黃彥霖 發表在 痞客邦 留言(3) 人氣()

基本上這次程式碼主要是用程式來產生 Sin 正弦波聲音,波形圖如下:

Sine_waves_different_frequencies  

以下為執行結果,可以使用滑桿來改變聲音頻率:

Unity - A.unity - New Unity Project 31 - PC, Mac & Linux Standalone  


using UnityEngine;

黃彥霖 發表在 痞客邦 留言(0) 人氣()

在 C# 寫入以下程式碼,並在腳本外部加入 Audio Source 的元件。
遊戲開始後即可聽見自己的麥克風聲音。

void Start () {
    audio.clip = Microphone.Start("Built-in Microphone", true, 1000, 44100);
    audio.pitch = 0.995f;
    audio.Play();
}

Unity - A.unity - New Unity Project 29 - PC, Mac & Linux Standalone  

黃彥霖 發表在 痞客邦 留言(4) 人氣()

using UnityEngine;
using System.Collections;

public class MyCamera : MonoBehaviour {
/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.

There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.

黃彥霖 發表在 痞客邦 留言(0) 人氣()

實際運行效果:

Unity - MainMenu.unity - New Unity Project 26 - PC, Mac & Linux Standalone

編輯環境效果:

Unity - MainMenu.unity - New Unity Project 26 - PC, Mac & Linux Standalone_2

請先在場景中建立五個 Plane 平面物件,之後在 Level Switch 中 Size 輸入5,把剛剛5個Plane平面物件一個一個拉進去。
之後在 Image 中輸入圖片數量(最好超過五個),再將各圖片拉進去。
之後按下執行,用鍵盤按左右建,即可看見圖片切換效果。

黃彥霖 發表在 痞客邦 留言(1) 人氣()

完成結果如下 (左下角會出現圓形小地圖,並會隨玩家移動):

Unity - Demo.unity - New Unity Project 24 - PC, Mac & Linux Standalone  

教學:

1. 請新建一個C#腳本,並取名為 Minimap,之後將下面程式碼複製到該腳本,最後再將該腳本套用至 Camera。

using UnityEngine;
using System.Collections;
 

黃彥霖 發表在 痞客邦 留言(6) 人氣()

按下 Esc 按鈕後,會自動暫停遊戲,並出現 ( 離開遊戲、畫面遊戲、回主選單 ) 的選項

using UnityEngine;
using System.Collections;
 
public class Pause : MonoBehaviour {
 
public string mainMenuSceneName;
public Font pauseMenuFont;
private bool pauseEnabled= false;

黃彥霖 發表在 痞客邦 留言(4) 人氣()

線上 JS→C# 轉換還不錯,記得類別名稱要改一下


http://www.m2h.nl/files/js_to_c.php





黃彥霖 發表在 痞客邦 留言(0) 人氣()

第一次寫這東西,中途遇到超多Bug,奮鬥了 13 小時,終於完成了...。回歸主題...

可以使用 本類別的 MyColorPicker.ShowColor 來取得選取的顏色,如下圖中間有一顆球體,我們為他加入 renderer.material.color = MyColorPicker.ShowColor; 的程式碼到 Update() 裡,可即時改變選取到的顏色。

Unity - A.unity - New Unity Project 10 - Android  

using UnityEngine;
using System.Collections;
 

黃彥霖 發表在 痞客邦 留言(0) 人氣()

用法:建立一個 C# 腳本,命名為 CameraFade ,之後將腳本拖曳 (套用) 至攝影機,將淡入淡出時間與顏色設定好(如下圖),之後再建立新腳本(或本身 CameraFade 類別)呼叫 FadeIn() FadeOut() 方法,即可做到淡入淡出效果!!

控制面板參數說明:

Fade In Time : 淡入時間,數值越大淡入時間越久
Fade Out Time : 淡出時間,數值越大淡出時間越久
Fade In Color : 淡入顏色
Fade In Color : 淡出顏色

Unity - Quit.unity - Death Hill Road - Android_2    

黃彥霖 發表在 痞客邦 留言(0) 人氣()

程式碼:

using UnityEngine;
using System.Collections;
 
public class TestVibrate : MonoBehaviour {
 
    AndroidJavaObject v;
 

黃彥霖 發表在 痞客邦 留言(0) 人氣()

    Unity 載入本地電腦檔案可以使用內建的 UnityEngine.WWW 的方式來載入,雖然比較方便使用,但是有很多副作用...,例如不能外加執行緒來增加效能,而且本身讀取超慢...,而且在Android中容易當機(讀太慢了 Android就會判斷成是沒有回應,故"強制"結束應用程式)。

    所以今天介紹 Bitmap 類別,他是 System.Drawing 套件(記得 using 匯入 && 專案中加入System.Drawing.dll 檔,DLL 在 Unity安裝路徑可找到)。

    廢話不多說,直接看結果吧!! 事先我先在 C:/C.jpg 路徑裡放了張圖片, 在執行前沒有載入任何貼圖,執行後他會抓 C:/C.jpg 影像來當作 Unity 的 Texture2D 。


執行前:

A  

黃彥霖 發表在 痞客邦 留言(11) 人氣()

JS 腳本必須放在  Standard Assets、 Pro Standard Assets、Plugins 這三個資料夾其中一個,名稱不可以改變或放在其他資料夾底下,這三個資料夾的程式碼會先編譯(官方內定),之後才編譯其他資料夾,所以不可以與C#放在同一塊。

之後調用最快方式是使用 static ,使用方法與傳統程式語言相同,還有一種是以 JSCode  jsc  =  gameObject.GetComponent("JSCode"); 來呼叫,我個人是推銷前者,因為比較方便。

 

 

 

 

 

 

 

黃彥霖 發表在 痞客邦 留言(1) 人氣()



C:\Users\<USER>\AppData\Roaming\Unity\Asset Store






黃彥霖 發表在 痞客邦 留言(1) 人氣()

本程式碼執行結果:

Unity - A.unity - New Unity Project 8 - Android  

程式碼:

using UnityEngine;
using System.Collections;
 
public class QuitGame : MonoBehaviour
{

黃彥霖 發表在 痞客邦 留言(1) 人氣()

取得所有可用 COM 連接埠名稱,如:COM3、COM5、COM7 ...等等

foreach (string com in System.IO.Ports.SerialPort.GetPortNames()) {//取得所有可用的連接埠
    print (com);
}




黃彥霖 發表在 痞客邦 留言(0) 人氣()

看這篇文章之前建議先看 移動滑鼠這篇:http://lolikitty.pixnet.net/blog/post/164570070

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class MouseControl : MonoBehaviour
{

    [DllImport("user32")]
    static extern bool SetCursorPos (int X, int Y);

    [DllImport("user32")]

黃彥霖 發表在 痞客邦 留言(0) 人氣()

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices; // 記得匯入

public class MouseControl : MonoBehaviour {

    [DllImport("user32")] // 使用 user32.dll ,這是系統的 Dll 檔,所以Unity會自動匯入,不用再手動加入 dll 檔
    static extern bool SetCursorPos(int X, int Y);

    void Start () {
        // 遊戲啟動後,會強制移動滑鼠到左上角。我這裡設定為: X = 0 、Y = 0,開發者可依需求設定移動座標。

黃彥霖 發表在 痞客邦 留言(0) 人氣()

using UnityEngine;
using System.Collections;
// 記得在 Unity 中指定使用 MicroSoft 的 .NET 套件 ( Edit → Project Setting → Player → Other Settings  → Api Compatibility → 選擇 .NET 2.0 )
using System.IO.Ports;

public class Test : MonoBehaviour {

    void Start () {
        SerialPort sp = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One); // 通訊埠為COM5、波特率(Baud rate) 9600
        sp.Open(); // 打開 COM5 通訊埠

        print("Sey : " + sp.ReadLine()); // 從單晶片輸出的信號會在Unity這裡印出

黃彥霖 發表在 痞客邦 留言(2) 人氣()

1. 最簡單實用的寫法。按下 Esc 按鍵後,瞬間關閉遊戲。如下程式碼:

if(Input.GetKeyDown("escape")){
    Application.Quit();
}

 
版本二可看這篇文章(多出按鈕&重置):Unity 入門教學: (手機可用) 結束遊戲(鍵盤ESC & 按鈕)、重置遊戲

 2. 比較華麗的效果。按下 Esc 按鍵後,會彈出是否結束遊戲的詢問視窗 (而且會淡出顯示)。

黃彥霖 發表在 痞客邦 留言(4) 人氣()