unity+android學習筆記–未完成的工作

var old:float; 
var news:float; 
var h:float; 
var d:float; 
var v:float; 
var a:float; 
function Start(){ 
  h=0; 
  d=0; 
  old=Input.acceleration.z; 
  news=0; 
  v=0; 

function FixedUpdate(){ 
 news=Input.acceleration.z;   
    a=news*9.81; 
    if(a>-10.2&&a<-9.7){            
      if(v>0.01&&v<-0.01){ 
        h+=v; 
      }            
    } 
    else if(a<=-10.2){ 
       h+=(0.5*Mathf.Abs(a)+v); 
       v=a+v;  
    } else{ 
       h+=(0.5*a+v); 
       v=a+v;  
    } 
    old=news;  

     
function Update () {       
 
   if(Input.GetKey(KeyCode.Escape)){ 
      Application.Quit(); 
   }   
      

function OnGUI(){  
    
    GUI.Label(Rect(100,210,100,100),"a:"+a); 
    GUI.Label(Rect(100,320,100,100),"h:"+h); 
    print(news); 

ChiziActivity.java:
[java]
package nate.dlnu; 
 
 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.View; 
import android.widget.TextView; 
 
public class ChiziActivity extends Activity { 
    /** Called when the activity is first created. */ 
     
    private CanvasView canvasView; 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        DisplayMetrics metric = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metric);  
//      float sw=metric.widthPixels;//獲取手機分辨率 
//      float sh=metric.heightPixels; 
        float densityDpi = metric.densityDpi;//160,240,320 
        float density = metric.density; //1,1.5,2; 
        System.out.println(density+"/"+densityDpi); 
        canvasView=new CanvasView(this,densityDpi);  
        setContentView(canvasView);  
    } 

class CanvasView extends View {  
       public float d; 
       public int i; 
       public CanvasView(Context context,float d1) { 
            super(context); 
            setFocusable(true); 
            setFocusableInTouchMode(true); 
            d=d1; 
        } 
      public void onDraw(Canvas canvas) { 
            Paint paint = new Paint(); 
            // 設置顏色 
            paint.setColor(Color.WHITE); 
            // 設置樣式-填充 
            //paint.setStyle(Style.FILL); 
            canvas.drawRect(new Rect(0, 0, 5000, 5000), paint); 
            // 設置顏色 
            paint.setColor(Color.BLUE); 
            // 繪文字 
            float dy=0.04f*d; 
            for(i=1;i<=30;i++){ 
                canvas.drawText(i+"", 70, i*dy*10, paint); 
                canvas.drawLine(0, i*dy*10, 70, i*dy*10, paint); 
                canvas.drawLine(0, i*dy*5, 50, i*dy*5, paint); 
            } 
            for(i=0;i<300;i++){ 
                canvas.drawLine(0, i*dy, 30, i*dy, paint); 
            } 
       } 

 

 

摘自 dlnuchunge的專欄

發佈留言