如何使用unity制作萌萌的live2d桌宠
10分钟内使用unity制作萌萌的桌宠
·
先看一下效果:
准备工作
环境(很重要): unity2018.4.24.f1 (unity2019无法用本文的方法实现背景透明,其他版本未测试),操作系统为Windows
相关包:包括Live2D_SDK_Unity_2.1.04_2_jp和Live2DFrameworkNeeds
live2d素材:本文使用官方的haru小姐姐,大家可以找自己感兴趣的素材。
(本文提供相关包和live2d素材)
环境配置
(unity live2d高手请略过)
- 下载Live2D_SDK_Unity_2.1.04_2_jp,将里面的framework,lib,tool文件夹拖入unity项目的assest中
- 导入Live2DFrameworkNeeds包
(导入包后,如果提示’AudioClip’ does not contain a constructor that takes 0 arguments,双击错误提示,找到错误代码处按照本节最后的代码进行修改即可) - 将live2d素材放入unity目录中的Resources文件夹(务必放在Resources里)
本文用到的相关SDK以及素材提取码:vet2
错误’AudioClip’ does not contain a constructor that takes 0 arguments修改方法
public static AudioClip LoadAssetsSound(string filename)
{
if(LAppDefine.DEBUG_LOG) Debug.Log( "Load voice : "+filename);
AudioClip player;
try
{
player = (AudioClip)(Resources.Load(filename)) as AudioClip;
return player;
}
catch (IOException e)
{
Debug.Log( e.StackTrace );
return null;
}
}
使用unity显示live2d人物
- 新建一个空对象用于显示人物,我将它取名为haru
- 为空对象添加以下组件:
Mesh Filter :并且指定Mesh为Live2D_Canvas(如果你没有这个请导入Live2DFrameworkNeeds包)
MeshCllider:在添加Mesh Filter 后再添加
L App Model Proxy:Path指定为live2d model的json文件,Scene No不用管(除非你知道它是啥)
My Game Controller:直接添加就好了
Audio Source:直接添加就好了
- 调整相机和live2d对象的角度和位置(下面是我的参数,没必要照搬)
- 运行游戏看看,与小姐姐互动一下,看看程序是否正常运行
背景透明
重头戏来了,首先新建一个C#代码,命名为TablePetBack,然后将下面代码粘贴进去(代码基于这篇博客修改)
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.IO;
public class TablePetBack : MonoBehaviour
{
public string strProduct;//项目名称
public int windowWidth;//窗口宽度
public int windowHeight;//窗口高度
private int currentX;
private int currentY;
#region Win函数常量
private struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll")]
static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);
[DllImport("Dwmapi.dll")]
static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
private const int GWL_EXSTYLE = -20;
private const int GWL_STYLE = -16;
private const int WS_EX_LAYERED = 0x00080000;
private const int WS_BORDER = 0x00800000;
private const int WS_CAPTION = 0x00C00000;
private const int SWP_SHOWWINDOW = 0x0040;
private const int LWA_COLORKEY = 0x00000001;
private const int LWA_ALPHA = 0x00000002;
private const int WS_EX_TRANSPARENT = 0x20;
private const int ULW_COLORKEY = 0x00000001;
private const int ULW_ALPHA = 0x00000002;
private const int ULW_OPAQUE = 0x00000004;
private const int ULW_EX_NORESIZE = 0x00000008;
#endregion
IntPtr hwnd;
// Use this for initialization
void Awake()
{
Screen.fullScreen = false;
#if UNITY_EDITOR
print("编辑模式不更改窗体");
#else
hwnd = FindWindow(null, strProduct);
//去边框并且透明
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);
//保持右下角间位置
currentX = Screen.currentResolution.width - windowWidth;
currentY = Screen.currentResolution.height - windowHeight;
SetWindowPos(hwnd, -1, currentX, currentY, windowWidth, windowHeight, SWP_SHOWWINDOW);
var margins = new MARGINS() { cxLeftWidth = -1 };
DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif
}
void OnApplicationQuit()
{
SetWindowPos(hwnd, -1, currentX, currentY, 0, 0, SWP_SHOWWINDOW);
}
}
新建一个空对象,将TablePetBack添加上去。
有3个参数需要设置
Window Width:桌宠宽度
Window Height:桌宠高度
StrProduct:程序名(需要与Player Setting里的游戏名一致,设置方法见下图)
将相机的Clear Flagss设置为Solid Color,颜色选择黑色。
大功告成,把项目build一下,然后运行,就可以一边敲代码,一边萌妹子互动了。
更多推荐
已为社区贡献1条内容
所有评论(0)