面试总结3

3.代码篇

  1. 第三角色控制器

    public class Player : MonoBehaviour {
        public Transform _cameraTrans;
        private Vector3 _cameraOffset;
        void Awake() {
            _cameraOffset = transform.position - _cameraTrans.position;
        }
        void Update() {
            _cameraTrans.position = transform.position - _cameraOffset;
        }
    }

  2. 计时器工具,从整点开始计时,格式为:00:00:00

private float timer = 0f;
private int h = 0;
private int m = 0;
private int s = 0;
private string timeStr = string.Empty;
void Update () {
timer += Time.deltaTime;
    if (timer >= 1f) {s++;  timer = 0;}
    if (s >= 60) {m++;s = 0;} 
if (m >= 60) {h++;m = 0;}
    if (h >= 99) {h = 0;}
}
void OnGUI(){
timeStr = string.Format ("{0:D2}:{1:D2}:{2:D2}", h, m, s);
    GUI.Label (new Rect (10, 10, 100, 200), timeStr);
}

3.unity3D从唤醒到销毁有一段生命周期,列出系统自己调用的重要方法。

Awake ()

OnEnable ()

Start()

FixedUpdate()

OnTriggerXXX(Collider other)

OnCollisionXXX (Collision collisionInfo)

Update()

LateUpdate ()

OnGUI()

OnDisable ()

OnDestroy ()



4.写一个角色控制器,鼠标控制屏幕晃动,鼠标控制开枪。

public class Player : MonoBehaviour {
    public GameObject _prefabBullet;
    private float _angleSpeed = 120f;
    void Update() {
        float eularY = Input.GetAxis("Mouse X") * _angleSpeed * Time.deltaTime;
        transform.Rotate(new Vector3(0, eularY, 0));
        if (Input.GetMouseButtonDown(0)) {
            Instantiate(_prefabBullet, transform.position, transform.rotation);
        }
    }
}

5.(1)写一个角色控制器,鼠标控制屏幕晃动,鼠标控制开枪。

(2)敌人AI,有各种状态,实现各种状态之间的切换。

(3)敌人会和主角对抗,敌人被打到之后,会闪一次红色,然后红色比例提升10%,10次攻击之后,成红色。

(4)敌人会自动攻击主角,主角也会有颜色变化。

(5)敌人会在范围内巡逻。

(6)UI,左边显示8个AI的被攻击次数,右边显示AI的攻击次数排序。

public class Player : MonoBehaviour {
    public Camera _camera;
    public GameObject _prefabBullet;
    private float _angleSpeed = 120f;
    //生命值
    private int _life = 10;
    //玩家的状态
    private bool _state = false;
    //是否被打到
    public void IsState() {
        if (_state && _life > 0) {
            _life -= 1;
            _state = false;
        } else {
            _state = true;
        }
    }
    public void RoleRotate() {
        float eularY = Input.GetAxis("Mouse X") * _angleSpeed * Time.deltaTime;
    }
    //风发射子弹
    public void RoleShoot() {
        if (Input.GetMouseButtonDown(0)) {
            Instantiate(_prefabBullet, transform.position, transform.rotation);
        }
    }
}
public class Enemy : MonoBehaviour {
    //敌人被打到状态
    private bool _state = false;
    //玩家与敌人距离
    private float _distance;
    //角色
    public GameObject _role;
    //生命值
    private int _life = 10;
    //敌人被攻击次数
    private int _timeAccack = 0;
    //敌人状态
    public void EnemyState() {
        if (_distance >= 10f) {
            if (_life >= 1 && _state == true) {
                _life -= 1;
                _timeAccack++;
            }
            _state = false;
        }
        else if (_distance >= 0 && _state == false) {
            _state = true;
        }
    }
    //敌人与玩家距离
    public void Distance() {
        _distance = Vector3.Distance(transform.position, _role.transform.position);
    }
    void OnGUI() {
        GUI.TextArea(new Rect(20, 50, 80, 30), _timeAccack.ToString());
        GUI.TextArea(new Rect(20, 90, 80, 30), _timeAccack.ToString());
        GUI.TextArea(new Rect(20, 130, 80, 30), _timeAccack.ToString());
    }
}

6.设计一个状态机类型,状态值为int类型,要求:

拥有接口,获取当前状态,切换状态

外部可以监听状态切换事件,参数为切换前状态和切换后状态(使用delete和event)//定义一个状态

public abstract class IAction{
    public int StateName;
    public IAction(int stateName) {
        this.StateName = stateName;
    }
    public int GetState(){
        return StateName;
    }

    public abstract bool CanGetIn();
    public abstract void GetIn();
    public abstract void GetOut();
    public abstract void Update(float dt);
}
//定义一个状态切换事件
public abstract class IEvent {
    public int code; 
    public IEvent(int code) {
        this.code = code;
}
    public int GetCode(){
        return code;
}
//当前状态事件检测
    public abstract bool Check();
}
public class AvatarStateMachine{
 		//初始化状态机
 		public void InitStateMachine(){}
 		//注册一个状态
 		public void AddAction(int action) {}
 		//注册一个状态切换事件
 		public void AddEventTransition(int fromAction, int toAction, IEvent byEvent) {}
 		//更新当前状态,检测能否进入下一个状态
 		public void UpdateStateMachine(){}
 		//强行切换状态
 		public void SwitchTo(int toState) {} 
}

7.如何实现以下人物在树丛中部分透明效果?

Shader "Custom/PlayerDiffuse" {
    Properties {
        _NotVisibleColor ("NotVisibleColor (RGB) ", Color) = (0.3,0.3,0.3,1)
        _MainTex ("Base (RGB) ", 2D) = "white"{}
    }
    SubShader {
        Tags { "Queue"= "Geometry+500""RenderType"="Opaque"}
        LOD 200 
        Pass {
            ZTest Greater
            Lighting Off
            ZWrite Off
          //Color [_NotVisibleColor]
            Blend SrcAlpha OneMinusSrcAlpha
            SetTexture [_MainTex] { ConstantColor [_NotVisibleColor] combine constant * texture } 
        } 
        Pass {
            ZTest LEqual
            Material {
                Diffuse (1,1,1,1)
                Ambient (1,1,1,1)
            }
            Lighting Off
            SetTexture [_MainTex] { combine texture } 
        } 
    } 
    FallBack "Diffuse"
}

8.Shader的代码实现?大概写一下

Shader "Custom/NewSurfaceShader"{
Properties {
        _Color ("Color", Color) = (1,1,1,1)
  	  _MainTex ("Albedo (RGB) ", 2D) = "white" {}
  	  _Glossiness ("Smoothness", Range(0,1)) = 0.5
  	  _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200 
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows
        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0
        sampler2D _MainTex;
        struct Input {
            float2 uv_MainTex;
        };
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
         }
         ENDCG
    } 
    FallBack "Diffuse"
}

9.​​​​​​​游戏中要怎么实现矩阵相乘?

//矩阵相乘
    public static float[][] Mul(float[][] a, float[][] b) {
        //确保矩阵a的列数和b的行数相等
        if(a[0].length != b.length) {
            return null;
        } 
        //用来存放结果的矩阵,axb的结果为a的行数和b的列数
        float[][] result = new float[a.length][b[0].length]; 
        //对a的每行进行遍历
        for(int i=0; i

你可能感兴趣的:(面试)