Unity 制作萌系live2d桌宠:屏幕自适应+交互
目录
- 准备工作
- 使用unity显示live2d人物
- 全屏+背景透明+点击穿透+置顶
- 屏幕自适应
- 交互
本文在之前的博客如何使用unity制作萌萌的live2d桌宠的基础上对项目继续改进,解决了屏幕自适应和交互的问题。
先来看看效果:
准备工作
准备工作和环境配置在上一篇博客已介绍,这里不再赘述。但还要提一点的是项目仍在 unity2018.4.24.f1(很重要) 的环境下开发(unity2019无法正常工作,其他版本未测试),操作系统为Windows。
使用unity显示live2d人物
上篇博客介绍过,这里简单说一下流程:
- 下载Live2D_SDK_Unity_2.1.04_2_jp,将里面的文件拷贝到unity的目录,然后导入Live2DFrameworkNeeds包
- 将live2d素材放入unity目录中的Resources文件夹
- 新建一个空对象用于显示人物,取名为haru(名字随意)
- 为空对象添加Mesh Filter、MeshCllider、L App Model Proxy、My Game Controller和Audio Source,相关参数参考上篇博客。
运行程序应该有下面的效果
全屏+背景透明+点击穿透+置顶
导出程序的时候必须使背景透明,否则就谈不上桌宠了。这里直接给出背景透明+点击穿透的代码,将此代码拖动到一个空对象上,然后把相机的Clear Flags设为Solid Color,并把背景颜色调成黑色。(代码基于这篇博客修改)
using UnityEngine;
using System;
using System.Runtime.InteropServices;public class TablePetBackSample : MonoBehaviour
{public string strProduct;//项目名称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 WS_POPUP = 0x800000;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;#endregionIntPtr hwnd;void Awake(){#if UNITY_EDITORprint("unity内运行程序");
#elsehwnd = FindWindow(null, strProduct);int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION);currentX = 0;currentY = 0;SetWindowPos(hwnd, -1, currentX, currentY, Screen.currentResolution.width, Screen.currentResolution.height, SWP_SHOWWINDOW);var margins = new MARGINS() { cxLeftWidth = -1 };DwmExtendFrameIntoClientArea(hwnd, ref margins);
#endif}
}
相机参数:
注意代码中strProduct一定要与Inspector种的Product Name一致,否则会出现奇怪的Bug(很重要,可能会影响unity导出的同名程序使用)。
效果如下:
可以看到背景是透明的,而且不妨碍鼠标网页的交互。
屏幕自适应
桌宠不能挡在中间影响我们正常使用电脑,我们需要让她处于右下角的位置并适应屏幕分辨率。首先我们需要把相机的Projection调整为Orthographic,
然后编写可以根据屏幕分辨率动态的调整桌宠的大小和位置的代码。代码如下,把代码拖到有live2d组件的对象上(即haru上)就可。
using UnityEngine;public class RolePositionControl : MonoBehaviour
{float rolewidth = 520;float roleheight = 520;private GameObject MainCamera;// Start is called before the first frame updatevoid Start(){MainCamera = GameObject.Find("Main Camera");}// Update is called once per framevoid Update(){//保持角色的大小和位置float frustumHeight = MainCamera.GetComponent<Camera>().orthographicSize * 2;float frustumWidth = frustumHeight * MainCamera.GetComponent<Camera>().aspect;float rolewidth = GetComponent<MeshFilter>().mesh.bounds.size.x;float roleheight = GetComponent<MeshFilter>().mesh.bounds.size.x;transform.localPosition = new Vector3((frustumWidth - rolewidth) / 2, -(frustumHeight - roleheight) / 2, transform.localPosition.z);}
}
效果如下:
交互
现在我们的桌宠只能看不能摸,也不会发出声音,下面我们来编写一些交互的代码。本人不熟悉unity的live2d编程,所以直接参照live2d框架中My Game Controller进行简单的改写。代码如下,为了不影响原来的包可以新建一个自己的My Game Controller组件(此处我命名为GameControl_X ),然后替换替换haru上的My Game Controller。
为何要改写My Game Controller?
原先的My Game Controller通过Input.GetMouseButton等接口获取鼠标状态,这样的方法在本项目中是无效的,前面的点击穿透功能会使程序无法检测到鼠标按下。所以这里需要使用windows接口来监测鼠标是否按下。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class GameControl_X: MonoBehaviour
{private static LAppLive2DManager instance;private float lastX = -1;private float lastY = -1;private GameObject MainCamera;//Windows接口[DllImport("user32.dll")]public static extern short GetAsyncKeyState(int vKey);private const int VK_LBUTTON = 0x01; //鼠标左键private const int VK_RBUTTON = 0x02; //鼠标右键//动画辅助keystring AniKey = "Begin";// Start is called before the first frame updatevoid Awake(){MainCamera = GameObject.Find("Main Camera");if (MainCamera != null){if (MainCamera.GetComponent<Camera>().orthographic){LAppLive2DManager.Instance.SetTouchMode2D(true);}else{Debug.Log("\"Main Camera\" Projection : Perspective");LAppLive2DManager.Instance.SetTouchMode2D(false);}}}// Update is called once per framevoid Update(){if (GetAsyncKeyState(VK_LBUTTON) != 0){if (AniKey == "Begin"){lastX = Input.mousePosition.x;lastY = Input.mousePosition.y;LAppLive2DManager.Instance.TouchesBegan(Input.mousePosition);AniKey = "Ani";}else{if (lastX == Input.mousePosition.x && lastY == Input.mousePosition.y){return;}lastX = Input.mousePosition.x;lastY = Input.mousePosition.y;LAppLive2DManager.Instance.TouchesMoved(Input.mousePosition);AniKey = "Begin";}}else{if (AniKey == "Ani"){lastX = -1;lastY = -1;LAppLive2DManager.Instance.TouchesEnded(Input.mousePosition);AniKey = "Begin";}}}
}
效果如下,鼠标按下后haru会看向鼠标的位置,也可以做点击、摸头等交互。
到此大功告成,懂live2d的同学还可以将haru替换成自己喜欢的萌物。
Unity 制作萌系live2d桌宠:屏幕自适应+交互相关推荐
- 如何使用unity制作萌萌的live2d桌宠
目录 准备工作 环境配置 使用unity显示live2d人物 背景透明 先看一下效果: 准备工作 环境(很重要): unity2018.4.24.f1 (unity2019无法用本文的方法实现背景透明 ...
- Unity 做成Live2D桌宠
其实这个还是挺难的(如果不用别人写好的话) 我直接把包发出来里面代码自行啃食 不看也可以 能用就好了! 下载了把包拖进去然后自动配置一下就可以把项目打包了 运行之后你的妹子就能在你的桌面上蹦跶了,想做 ...
- Unity使用UIStretch做屏幕自适应遇到clipped panels must have a uniform scale
clipped panels must have a uniform scale,or clipping won't work properly! Unity如果还在使用UIStretch做屏幕自适应 ...
- 如何从零开始制作智能桌宠?
作者 | 李秋键 责编 | maozz 出品 | CSDN(ID:CSDNnews) 引言:是否还记得曾经风靡全国的QQ宠物呢,那个又调皮又可爱的QQ企鹅,偶尔还会生病撒娇,需要培养的虚拟宠物,你是否 ...
- 手把手教你制作智能桌宠(小可爱哦!)
大家曾经记忆里的回忆,是不是腾讯企鹅的.它又萌又可爱,如图: 但是我们多么想制作一款自己的智能宠物啊,今天我们就将带你手把手制作桌宠.最主要的是文末我将给出源代码哦!大家可以DIY设计自己专属的桌面宠 ...
- python桌面宠物_如何从零开始制作智能桌宠?
作者 | 李秋键 责编 | maozz 引言:是否还记得曾经风靡全国的QQ宠物呢,那个又调皮又可爱的QQ企鹅,偶尔还会生病撒娇,需要培养的虚拟宠物,你是否还记得它的样子呢.它是腾讯公司推出的第一款QQ ...
- ppet配合live2d实现DIY桌宠-可爱的猫(支持Linux,windows,mac)
看到某博客中的js猫还会跟随鼠标动 衍生偷猫想法,正好这几天在帮女朋友找桌宠 ps:利用PPET对接live2d做DIY桌宠 这是偶然看到的某博客的猫 于是看了下该博客的源代码,顺腾摸瓜找到了这个 这 ...
- Python制作智能桌宠2
Python制作新一款智能桌宠 引言:了解过我们之前文章的都知道我们曾经做过一个智能桌宠项目.但是很显然那个程序过于卡段.故这一次我们将重新制作个智能桌宠项目,不同于之前的项目在于,之前使用了大量的j ...
- python写桌宠_【Unity/开源】八重樱桌宠(三):Python自动更新、修复描边
接下来就到了喜闻乐见的PY环节: defget_html(url):try:headers = { "Host": "github.com","Use ...
最新文章
- strcpy +memcpy实现循环右移
- fon循环总是返回最后值问题
- 文件复制函数的效率比较
- PHP中的$_SERVER['PATH_INFO']
- Linux下部署LVS(DR)+keepalived+Nginx负载均衡
- 你大爷还是你大爷!三星震撼首发折叠屏智能手机Galaxy Fold
- Mysql 命令行控制事务
- ThreadLocal的作用
- 《编程之美》中买书问题算法。空间复杂度O(n),时间复杂度O(n),求挑战
- 中国人和犹太人做生意的区别
- 免费SSL证书申请和部署
- c4d里.gil和.gi2是什么文件?怎么打开
- CSS3动画实现高亮光弧效果,循环闪动效果
- 现役大学生必看!干货满满!
- (7)3DMAX之倒角剖面修改器、扫描修改器、倒角剖面与扫描之间的区别
- 西门子精智HMI-TP1200发邮件功能
- 【技巧】windows剪切板
- je学习笔记一:jsp页面
- markdown化学方程式
- 在线引流工具Tcpcopy原理、使用、采坑