2015年3月31日 星期二

TCP: Server / Client

Java (Server):

package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket; 
import java.util.Date;
  

public class Server  implements Runnable {
      private Socket clientSocket;
      Server (){
          ServerSocket ss = null;
          try {
            ss = new ServerSocket(1800);
            System.out.println("[▶] 伺服器已啟動,等待連線中...");
          } catch (IOException e) {
        }

     while (true) {
            try {
                Socket s = ss.accept();
                new Thread(this).start();
            } catch (IOException e) {
            }
       }
    }

    @Override
    public void run() {
        
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStreamWriter osw = null;
        PrintWriter wr = null;
        String ip = null;
        
               try {
                System.out.println("------------------------------------------");
                ip = clientSocket.getInetAddress().toString();
                System.out.println("連線IP : " + ip + ":" + clientSocket.getPort());
                System.out.println("連線時間 : " + new Date());
                isr = new InputStreamReader(clientSocket.getInputStream());
                br = new BufferedReader(isr);
                String msg = br.readLine();
                osw = new OutputStreamWriter(clientSocket.getOutputStream());
                wr = new PrintWriter(osw, true);
                System.out.println("傳來的值:" + msg);
               // 遊戲邏輯,對接收到的值之後進行分類,再依需求做處理。
            } catch (IOException e) {
            }
    }
    
        public static void main(String [] args) {
         new Server ();
   }
}

--------------------------------------------------

Client (Unity C#):

using System; 
using System.Net.Sockets; 
using System.IO; 
using UnityEngine;
using System.Collections;

public class TcpClientScript : MonoBehaviour{

Socket socket;
StreamReader sr;

byte[] bate, aaa;

TcpClient tcpClient;
NetworkStream ns;

public void setClinetStart(string ip, int port){

try {

tcpClient = new TcpClient (ip, port);
NetworkStream ns = tcpClient.GetStream (); 
StreamWriter sw = new StreamWriter (ns); 
sw.WriteLine ("Unity C# 傳來的訊息");           
sw.Flush (); 
StreamReader sr = new StreamReader (ns); 
print ("從 Java服務器接收到的訊息:" + sr.ReadLine ());
} catch (Exception e) {
print (e); 
Application.Quit();
}
}
}


2015年3月16日 星期一

Unity5.0 Animator 建立粒子系統.....

操作方法:  

1. Gameobjcet 新建一個Animation,Unity5.0 會自動建立Animator。
2. Animator視窗進去後,會看到橘色的Animation(設計者建立)。
3. Inspector 視窗底下會看到Add Behaviour 按鈕點下去,會讓設計者尋找腳本。
4. 若沒有腳本選擇New Script後打上Script name 後 點擊Create and add 按鈕建立腳本。
5. 以下程式函式Unity本身就會建立,只是要注意下函式不同的用法即可。

-------------------------------------------------------------------

程式碼部分:

using UnityEngine;
using System.Collections;

public class SpecialAttackParticlesSmb : StateMachineBehaviour {

public GameObject particles;   
public AvatarIKGoal attackLimb; 

private Transform particlesTransform; 
private ParticleSystem particleSystem; 

override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {

if(particlesTransform != null)
return;

GameObject particleInstance = Instantiate (particles);
particlesTransform = particleInstance.transform;
particleSystem = particleInstance.GetComponent<ParticleSystem> ();
}

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
particleSystem.Stop ();
}

override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {

if(particleSystem == null || particlesTransform == null)
return;

Vector3 limnPosistion = animator.GetIKPosition (attackLimb);
Quaternion limbRotation = animator.GetIKRotation (attackLimb);

particlesTransform.position = limnPosistion;
particlesTransform.rotation = limbRotation;

if(!particleSystem.isPlaying)
particleSystem.Play();

}
}

-------------------------------------------------------------------

結果圖: