Google Code Prettify

2013年12月12日 星期四

(轉)

Unity 匯入一個有動作的角色,設定位置後仍在原地動。
原因是因為Animator的Apply Root Motion被打勾,所以才在原地動。
GameObject gobj = Instantiate(Walker, new Vector3(vecWalker.x, 0, vecWalker.y), Quaternion.identity) as GameObject;
  gobj.transform.parent = friendLayer.transform;

轉自 

akira32 編程之家 Yahoo

2013年11月19日 星期二

對軸心旋轉

準備任意張圖,各自做成物件,在做一個要當作軸心的物件(或是用空物件代替),並將CODE貼到空物件上。


#pragma strict

var tex:GameObject[] = new GameObject[5];
var speed:int = 40;

function Start () {

for(var i:int =0 ; i {
var x;
var y;
//圆点坐标:(x0,y0)
//半径:r
//角度:a0
//则圆上任一点为:(x1,y1)
//x1   =   x0   +   r   *   cos(ao   *   3.14   /180   )
//y1   =   y0   +   r   *   sin(ao   *   3.14   /180   )

x= 0+ 3*Mathf.Cos(i*72 * 3.14/180);
y= 0+ 3*Mathf.Sin(i*72 * 3.14/180);

tex[i].transform.position = Vector3(x,0,y);

}
}

function Update () {

tex[0].transform.RotateAround(this.transform.position, Vector3.up, speed * Time.deltaTime);
tex[0].transform.LookAt(this.transform);
tex[1].transform.RotateAround(this.transform.position, Vector3.up, speed * Time.deltaTime);
tex[1].transform.LookAt(this.transform);
tex[2].transform.RotateAround(this.transform.position, Vector3.up, speed * Time.deltaTime);
tex[2].transform.LookAt(this.transform);
tex[3].transform.RotateAround(this.transform.position, Vector3.up, speed * Time.deltaTime);
tex[3].transform.LookAt(this.transform);
tex[4].transform.RotateAround(this.transform.position, Vector3.up, speed * Time.deltaTime);
tex[4].transform.LookAt(this.transform);

}

===============================================================
成果
 物件就會對著設定的軸心(在這裡是空物件)做旋轉了。








2013年11月16日 星期六

unity點擊特效

最近同事提到有的遊戲點下去就會出現一個類似水波的效果,問說要怎麼實現。回家就稍微做了一下。

準備一個特效(會自動銷毀的特效),把SCRIPT放到鏡頭上,並且把特效拖到MyEffect上

#pragma strict
//effect
var MyEffect:GameObject ;
var hit : RaycastHit;
var cooldown : float;
private var effect:GameObject;
private var g_camera : Camera;
private var g_camTransform : Transform;

function Awake()
{
   g_camera = Camera.main;
   g_camTransform = g_camera.transform;
}

function Update ()
{
if(cooldown>0){cooldown-=Time.deltaTime;}
var mousePos = Input.mousePosition;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var vecMousePosition = g_camera.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y,1));
if(Input.GetMouseButton(0) &&cooldown<=0)
{
                 //生成特效
effect=Instantiate(MyEffect, vecMousePosition , Quaternion.identity);
cooldown=0.15;
}
}




=================================================
成果
 OK 這樣子點擊的時候就會出現特效了。
範例



2013年11月14日 星期四

Unity Shader學習筆記(1)

最近看著書學寫Unity的Shader,順便寫一下筆記
兩個顏色混合

Shader "Custom/ex1" {
Properties {
_EmissiveColor ("ColorA", Color) = (1,1,1,1)
_AmbientColor ("ColorB", Color) = (1,1,1,1)
_PowerValue ("Power", Range(0,10)) = 2.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Lambert

float4 _EmissiveColor;
float4 _AmbientColor;
float _PowerValue ;

struct Input {
float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
float4 c;
c = pow((_EmissiveColor + _AmbientColor), _PowerValue );
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}


=============================================================
成果


2011年4月23日 星期六

#define 用法

在書上都會看到一些 #define的用法
但是通常都很短
但是再寫WINDOWS程式的時候
常會看到 以下這種寫法

#define CreateWindowA(lpClassName, lpWindowName, dwStyle, x, y,\ nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)\ CreateWindowExA(0L, lpClassName, lpWindowName, dwStyle, x, y,\ nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)


所以上網查了一下 原來 #define 有多行的用法

3.define的多行定義
define可以替代多行的代碼,例如MFC中的宏定義(非常的經典,雖然讓人看了噁心)
#define MACRO(arg1, arg2) do { \
/* declarations */ \
stmt1; \
stmt2; \
/* . .. */ \
} while(0) /* (no trailing ; ) */
關鍵是要在每一個換行的時候加上一個"\

2011年4月22日 星期五

callBack function 試做

找了一下資料 發現網路上的範例都寫得頗難。

自己寫看看

#include<iostream>
using namespace std;

typedef int(*myFunc)(int a);

int print(int a)
{
a=a+10;
return a ;
}

int test(myFunc my)
{
int a=9;
int b=0;
b=my(a);
return b;
}

int main()
{
int num=0;
num=test(print);
cout<<num<<endl;
system("pause");
return 0;
}
===================================================
簡單的說,如果你使用了某個function,那麼你就是『call』了一個function。如果系統或是函式是要求你給一個function pointer,這個function pointer指到一個實際的函式(多半這個函式是你自己寫的)。然後它會在適當的時間呼叫此function,則此function就是所謂的 callback function。因為這個function是被『callback』了。

=========================================================
文章取自:井民全
Callback = Call back = Call me back when you have done the job = 回電﹝回個電話﹞ = 做好了請叫我一下。 Call back 是英語世界的日常用語。 有一天,內地的A想請港口的B搬貨櫃,A拿起手機呼叫B: A: 呼叫B!請把港口的貨櫃搬上輪船,搬完後請回電告訴我一共搬了幾櫃。 B: 電話幾號? A: 0123456789。 於是B開始搬貨櫃,搬完後打了那個電話號碼,告訴A一共搬了幾櫃。 在程式裡面使用C實作這種call back機制時,A必須傳一個function pointer 給B,讓B可以使用這個function pointer來通知A。這個function pointer 內含某個function的位址,而這個function就被稱為callback function, 並不是因為這個function的長相特別,只是因為它的用途是特別設計給B來 回呼而已。 先了解function pointer的使用,就可以了解如何使用callback了。 有些人對於C語言沒給callback function一個特別的長相有點不滿意,所以 特別定製了一個modifier﹝修飾字﹞叫做CALLBACK,冠在函式名稱前面 以顯名號,就是要告訴看程式碼的人:「這是一個callback function! 這樣 講你應該就知道它是做什麼的了吧。請看看文件或找找程式碼,到底是誰會 傳這個callback function給哪些人......這可是精心設計的。機關重重,請 小心謹慎。」 會需要設計出callback機制的場合,通常比較特殊,例如 iterative processing 或非同步的程式。Tree traversal是iterative processing的一個例子,當然 也只有把tree traversal通用化才會使用到callback。非同步就例如non-blocking socket、non-blocking I/O、或多執行緒程式了。

2011年4月19日 星期二

簡單的atoi實作

上班的時候,剛好有做到類似的程式
記錄下來,比較不會忘記。
簡單的做一下,將字串轉成數字。

#include <stdio.h>
#include <string.h>
#include<iostream>

using namespace std;

int myatoi(const char*);
void main()
{
char test[48]="12345";
int num=0;
num=myatoi(test);
cout<<num<<endl;
system("pause");
}

int myatoi(const char* str)
{
int result=0;
while(*str!='\0')
{
result*=10;
//把字元-'0' 就會變成數字 和 -48同意義
result+=*str-'0';
str++;
}
return result;
}