动作游戏Demo(一)换装系统

本文介绍如何在Unity中实现角色的自定义,包括模型资源导入、UI界面制作、模型自旋转及头部、手部的换装功能。此外还详细介绍了如何通过脚本实现颜色更换和随机颜色生成,并保存加载换装信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 列表内容

1.导入模型资源文件,制作角色和武器的Prefab

  • 以导入player为例。
  • (1)导入FBX文件(注意别导入3dmax文件)
  • (2)骨骼设置(Rig):设置成导入为Hunmanoid(人体动画)。对骨骼节点进行匹配校验(一般不做修改)。

2.制作UI界面

本项目使用的是NGUI插件。(利用anchors来解决屏幕自适应)
这里写图片描述

3.模型自旋转

  • 在场景中选中palyer,window-animation,保存。
  • 在animation面板下选择rotation.y,本案例以2秒围绕y轴旋转360度。
    这里写图片描述

4. 头部和手部的换装

  • 以头部换装为例。
using UnityEngine;
using System.Collections;

public class MenuContorller : MonoBehaviour {
public SkinnedMeshRenderer headMeshRenderer;  //蒙皮网格渲染器
public Mesh[] headMeshArray;  //获得可以替换的Mesh
private int headMeshIndex =0;  //当前索引(用的哪个Mesh)

public void OnHeadNextClick() {  //NGUI需要将该方法注册到指定的Button上面
        headMeshIndex++;
        headMeshIndex %= headMeshArray.Length;  //很好用的范围间循环计时器(自身求余)
        headMeshRenderer.sharedMesh = headMeshArray[headMeshIndex];  //sharedMesh用于蒙皮
    }
}

5.颜色的更换/随机颜色

  • 以换成紫色为例。
using UnityEngine;
using System.Collections;

public class MenuContorller : MonoBehaviour {
public Color purple;  //unity中无该颜色默认值,需在Inspector面板上赋值。其他颜色均可用Color.blue

public void OnPurpleClick() {
        OnColorChange(purple);
    }
void OnColorChange(Color c) { 
    foreach(SkinnedMeshRenderer skin in SkinnedMeshRendererArray){
        skin.material.color = c;   //materical.color
    }
}
}
  • 随机颜色
   public void RandomColorClick()
    {
        float r = Random.Range(0f, 1f);   //只能0-1的float
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);
        float a = Random.Range(0f, 1f);
        Color color = new Color(r, g, b,a);     //也可以只(r,g,b)
        OnColorChange(color);
    }
    void OnColorChange(Color c) { 
    foreach(SkinnedMeshRenderer skin in SkinnedMeshRendererArray){
        skin.material.color = c;        //materical.color
    }
}

6. 换装信息系统的保存和加载

  • 以保存随机颜色和紫色为例
using UnityEngine;
using System.Collections;
using System.Collections.Generic;    //用到List<>需要引用该头文件
using UnityEngine.SceneManagement;   //新加载场景的API 需引用该头文件

public class MenuContorller : MonoBehaviour {
public Color purple;
public List<Color> colorArray;
private int colorIndex = -1;  //-1为默认值 即默认当前颜色

void Start() {
        colorArray = new List<Color> { Color.blue,Color.green,Color.cyan,Color.red,purple};   //使用List<>相比于数组color[],可以动态存储数据。  利用list.Add()添加数组;
        DontDestroyOnLoad(this.gameObject);     //需要取到该脚本的索引值,不能销毁。
    }

public void OnPurpleClick() {
        OnColorChange(purple);
        colorIndex = 4;  //存储索引值
        }
public void RandomColorClick()
    {
        float r = Random.Range(0f, 1f);   //只能0-1的float
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);
        float a = Random.Range(0f, 1f);
        Color color = new Color(r, g, b,a);     //也可以只(r,g,b)
        OnColorChange(color);
        colorArray.Add(color);   //添加到集合里面
        corlorIndex =5;
    }        
void OnColorChange(Color c) { 
    foreach(SkinnedMeshRenderer skin in SkinnedMeshRendererArray){
        skin.material.color = c;   //materical.color
    }
}
     void Save() {
        PlayerPrefs.SetInt("colorIndex", colorIndex);
    }
     public void OnPlayClick() {  //按下play加载下个场景
        Save();
        SceneManager.LoadScene(1);  //新API
    }
} 
http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616 html5 挺火,写个html5游戏玩玩吧,想起cocos2d 貌似各个平台都有,网上找了找,下载了个Cocos2d-html5引擎包。 貌似另个开源引擎lufylegend.js也很好,下次用用lufylegend.js试试。 开发环境 chrome Cocos2d-html5 游戏地址:http://hehe108.sinaapp.com/cocos2d/ (adsw回车) 实现方法如下 1.创建好 LayerGradient的子类 (里面放坦克子弹) 2.重写 onEnter 方法添加些基本按钮 跟些初始坦克,子弹 3.通过schedule方法 控制 坦克 子弹的重画 4.根据键盘按键(ASWD)确定出坦克的方向,根据坦克的方向修改坦克的X,Y轴坐标,来实现坦克的移动 5.通过cc.rectIntersectsRect函数来进行碰撞检测,实现子弹打击坦克 具体代码 1.在项目里面添加方向 var Direction = { L:0, U:1, D:2, R:3, STOP:4 }; 2.添加子弹类的相关属性 SPEED:10, WIDTH:15, HEIGHT:15, x:null, y:null, dir:null, live:true, tankClient:null, //LayerGradient子类TankClient 的引用 good:null, 3.子弹初始化,重画 ctor:function (x,y,good,dir,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.dir=dir; this.tankClient=tankClient; this.good=good; this.initWithFile(s_missile); this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if(!this.live){ this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); this.Move(); }, 4.添加子弹打击坦克的方法 HitTank:function(t){ if (cc.rectIntersectsRect(this.GetRectangle(), t.GetRectangle()) && t.live && this.live && this.good != t.good){ t.live = false; this.live = false; return true; } return false; }, 5.添加坦克类相关属性 SPEED:5, WIDTH:58, HEIGHT:58, x:0, y:0, l:false, u:false, r:false, d:false, dir:Direction["STOP"], ptDir:Direction["D"], tankClient:null, good:null, step:0, live:true, 6.在tank类中 坦克初始化,重画 ctor:function (x,y,good,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.tankClient=tankClient; this.good=good; if(good){ this.initWithFile(s_tank); }else{ this.initWithFile(s_enemy); } this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if (!this.live){ if (!this.good){ this.tankClient.removeChild(this, true); } this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); switch (this.ptDir) { case Direction["D"]: this.setRotation(0); //旋精灵控制 炮筒方向 break; case Direction["U"]: this.setRotation(180); break; case Direction["L"]: this.setRotation(270); break; case Direction["R"]: this.setRotation(90); break; } this.Move(); }, 7.tank发子弹的方法 Fire:function() { if(!this.live) return null; for(var i=0;i<this.tankClient.missiles.length;i++){ var m = this.tankClient.missiles[i]; if (m.live == false){ m.x=this.x; m.y=this.y; m.live=true; m.dir=this.ptDir; m.good=this.good; this.tankClient.addChild(m); return m; } } var missile=new Missile(this.x,this.y,this.good,this.ptDir,this.tankClient); this.tankClient.missiles.push(missile); return missile; }, 8.LayerGradient加入坦克 this.tanks = []; this.myTank = new Tank(60,20, true, this); for (var i = 0; i < 10; i++){ this.tanks.push(new Tank(50 + 70 * (i + 1), 420, false, this)); } 9.LayerGradient中调用子弹打击坦克的方法 for(var i=0;i<this.missiles.length;i++){ var m = this.missiles[i]; m.HitTank(this.myTank); m.HitTanks(this.tanks); m.Draw(); } 10.控制坦克移动射击的部分代码 onKeyUp:function(key) { this.myTank.KeyReleased(key); }, onKeyDown:function(key) { this.myTank.KeyPressed(key); } 11.用Ant和compiler合并压缩js后发布到sae 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值