3D game第二次作业
对象和资源的区别联系
游戏对象是指在游戏中玩家可以操控并看到的物体,贴图,背景对。
游戏资源则是组成游戏的成分,可以是对象,也可以是代码,放在资源库中,不一定要被看到或者加载出来。
联系:对象是是由各种资源组合而成。
资源组织结构
以全面战争:战锤2为例:
普通插件:DirectX,vcredist
基础资源:代码,声音,图像。
DLC:DLC资源,DLC代码。
辅助空间:本地存储,云存储,截屏
虚拟纹理
对象组织结构
有摄像机,背景,光线,对象实体。
用debug语句理解事件
用空对象加入代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestifyBeh : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("This Start!");
}
// Update is called once per frame
void Update()
{
//Debug.Log("This Update!");
}
void Awake()
{
Debug.Log("This Awake!");
}
void FixedUpdate()
{
Debug.Log("This FixUpdate!");
}
void LateUpdate()
{
Debug.Log("This LateUpdate!");
}
void OnGUI()
{
Debug.Log("This OnGUI!");
}
void OnDisable()
{
Debug.Log("This Disabled!");
}
void OnEnable()
{
Debug.Log("This Enabled!");
}
}
结果:
后面会有一大串的FixUpdate,LateUpdate,Update,OnGUI的乱序循环。
查手册解释对象
GameObject:统一场景中所有实体的基类。
Transform:物体的位置、旋转和比例。
Conponent:所有附加到游戏对象的基类。
预设和克隆
预设的好处:预设可以在场景中多次使用。如果对预设进行更改,所有对预设的应用都会被更改。
预设与克隆的区别:预设对象会随着本体的改变而改变,克隆体则不会随母体而变化。
table预设代码:
using UnityEngine;
public class InstantiationExample : MonoBehaviour {
// Reference to the Prefab. Drag the table into this field in the Inspector.
public GameObject myPrefab;
void Start()
{
Instantiate(myPrefab);
}
}
GUI编程小游戏
只用GUI做井字棋小游戏,内置了重玩,推出的案件。下错棋了会显示三秒错误信息,因此具有时间系统。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public Texture2D back;
public Texture2D background;
public Texture2D empty;
public Texture2D c1;
public Texture2D c2;
public Texture2D noicant;
public Texture2D winwin;
public Texture2D over;
public Texture2D reset;
int[,] map=new int[4,4];
public static float w,h;
float bl;
int round;
int status;
int winner;
int step;
float nowt,beforet;
void Start()
{
for(int i=1;i<=3;++i)
for(int j=1;j<=3;++j)
map[i,j]=0;
w=Screen.width;
h=Screen.height;
round=1;
status=0;
step=0;
}
void OnGUI(){
GUI.Label(new Rect(0,0,(int)(w*1.3f),(int)(h*1.3f)),back);
if ( GUI.Button(new Rect(w-100,0,100,100),c2)){
Application.Quit();
}
float xpos=(w-bl)/2;
GUI.Label(new Rect((int)xpos,0,bl,bl),background);
float sx,sy;
sx=xpos+0.05f*bl;
sy=0.05f*bl;
float len=0.3f*bl;
float x,y;
for(int i=1;i<=3;++i){
for(int j=1;j<=3;++j){
x=sx+len*(i-1);
y=sy+len*(j-1);
if (map[i,j]==0){
if ( GUI.Button(new Rect((int)x,(int)y,(int)len,(int)len),empty) ){
if (status!=100&&status!=50){
map[i,j]=round;
if (round==1) round=2;
else round=1;
step++;
}
}
}else if(map[i,j]==1){
if ( GUI.Button(new Rect((int)x,(int)y,(int)len,(int)len),c1) ){
if (status!=100&&status!=50)
status=404;
}
}
else if (map[i,j]==2){
if (GUI.Button(new Rect((int)x,(int)y,(int)len,(int)len),c2) ){
if (status!=100&&status!=50)
status=404;
}
}
}
}
if (GUI.Button(new Rect((int)(0.5f*w-0.15f*h),(int)(h*0.825f),(int)(0.3f*h),(int)(0.15f*h) ),reset) ){
Reset();
}
if (status==405){
GUI.Label(new Rect((int)(w*0.01f),(int)(h*0.5f-w*0.16f),(int)(w*0.08f),(int)(w*0.16f)),noicant);
}
if (status==100){
GUI.Label(new Rect((int)(w*0.91f),(int)(h*0.5f-w*0.16f),(int)(w*0.08f),(int)(w*0.16f)),winwin);
}
if (status==50){
GUI.Label(new Rect((int)(w*0.91f),(int)(h*0.5f-w*0.16f),(int)(w*0.08f),(int)(w*0.16f)),over);
}
}
void Update()
{
w=Screen.width;
h=Screen.height;
bl=min(w,h);
bl=bl*0.8f;
nowt=Time.time;
if (status==404){
beforet=nowt;
status=405;
}
if (nowt-beforet>3){
status=0;
}
winner=win();
if (winner!=0){
status=100;
}else if (step==9){
status=50;
}
}
int win(){
for(int i=1;i<=3;++i){
if (map[i,1]==map[i,2] && map[i,2]==map[i,3] && map[i,3]!=0)
return map[i,1];
}
for(int i=1;i<=3;++i){
if (map[1,i]==map[2,i] && map[2,i]==map[3,i] && map[3,i]!=0){
return map[1,i];
}
}
if (map[1,1]==map[2,2] && map[2,2]==map[3,3] && map[2,2]!=0){
return map[2,2];
}
if (map[1,3]==map[2,2] && map[2,2]==map[3,1] && map[2,2]!=0){
return map[2,2];
}
return 0;
}
void Reset(){
for(int i=1;i<=3;++i)
for(int j=1;j<=3;++j)
map[i,j]=0;
step=0;
round=1;
status=0;
}
float min(float a,float b){
if (a<b) return a;
return b;
}
}
效果:
项目亮点
1.所有图片的尺寸和位置均用 Screen.width和Screen.height作为尺标,以一定比例确定位置。这样即使换了分辨率,也能是图片的尺寸和位置一起变换
例如代码:
GUI.Button(new Rect((int)(0.5f*w-0.15f*h),(int)(h*0.825f),(int)(0.3f*h),(int)(0.15f*h) ),reset)
就规划了重来按钮的尺寸和位置。
2.采用了纯图片的设计,更加美观。
纯图片还方便了拉伸
(据实验发现,unity 3D图片通过调整GUI.Label,GUI.Button等只能自动缩小,不能自动放大,因此初始图片大一点为好)
3.游戏加入了计时系统和终止按钮。
当点击错误(重复落子)时,左边会提示“否”,并显示3秒。胜利或则平局或重玩会立即结束“否”的显示。
右上角的X是关闭游戏(项目导出后才生效)
如下图: