unity上传头像_Unity用户自定义圆角头像
前天朋友遇到一个这样的需求,而且比较棘手让我帮忙解决。需求就是棋牌类的游戏,玩家的个人资料中包括自己的头像而且可以浏览相册中的图片或者使用相机拍照设置。关于这个问题我也查阅一些资料,由于涉及安卓部分知识,首先要了解Unity和安卓是如何通信的。
首先看到的是雨松老师的博客:http://www.xuanyusong.com/archives/1480咱们可以一起参考下这篇博客。好了,废话就不多说了,夜已深,开始撸代码吧!
1 新建空的工程
2,头像显示当然要用图片了,就用UGUI的RawImage吧。
3,准备工作基本上结束了,接下来就是完成浏览相册和拍照上传了。这部分就不详细介绍了网上有很多教程,借助Eclipse完成浏览相册和拍照的逻辑,导出jar包在Unity中调用即可。插件已经导出,稍后会把工程共享给大家!
接下来该调用浏览相册和拍照的功能了,简单的测试,就这样吧!
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 100, 100), "Take Photo"))
{
OpenCamera();
}
if (GUI.Button(new Rect(220, 10, 100, 100), "Photo view"))
{
OpenPick();
}
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home))
{
Application.Quit();
}
}
4,既然是圆角头像,那么还要处理圆角了。首先想到的是Shader做个裁剪即可。
是不是很神奇,上一秒还是方的哦!
5,接下来开始测试了,要打包真机测试的,请稍后打包中,,,,
6,哇咔咔,接下来要真机测试了。
对了,上面日志显示两个图片字节数值而且相差很大的,你看到的是对的。由于要考虑网络传输,传输的数据包要尽可能的小,这样才会节省流量和提高效率,第一个值是图片真是的字节数,第二个数值表示压缩后的图片字节数:2119,除以1024也就是2.07kb。这个数据还是可以接受的。
接下来是压缩的实现方法:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Threading;
public class TextureTools : MonoBehaviour
{
public class ThreadData
{
public int start;
public int end;
public ThreadData(int s, int e)
{
start = s;
end = e;
}
}
private static Color[] texColors;
private static Color[] newColors;
private static int w;
private static float ratioX;
private static float ratioY;
private static int w2;
private static int finishCount;
private static Mutex mutex;
public static Texture2D Point(Texture2D tex, int newWidth, int newHeight, bool flipH, bool flipV)
{
return ThreadedScale(tex, newWidth, newHeight, false, flipH, flipV);
}
public static Texture2D Bilinear(Texture2D tex, int newWidth, int newHeight, bool flipH, bool flipV)
{
return ThreadedScale(tex, newWidth, newHeight, true, flipH, flipV);
}
private static Texture2D ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear, bool flipH, bool flipV)
{
texColors = tex.GetPixels();
newColors = new Color[newWidth * newHeight];
if (useBilinear)
{
ratioX = 1.0f / ((float)newWidth / (tex.width - 1));
ratioY = 1.0f / ((float)newHeight / (tex.height - 1));
}
else
{
ratioX = ((float)tex.width) / newWidth;
ratioY = ((float)tex.height) / newHeight;
}
w = tex.width;
w2 = newWidth;
var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
var slice = newHeight / cores;
finishCount = 0;
if (mutex == null)
{
mutex = new Mutex(false);
}
if (cores > 1)
{
int i = 0;
ThreadData threadData;
for (i = 0; i < cores - 1; i++)
{
threadData = new ThreadData(slice * i, slice * (i + 1));
ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
Thread thread = new Thread(ts);
thread.Start(threadData);
}
threadData = new ThreadData(slice * i, newHeight);
if (useBilinear)
{
BilinearScale(threadData);
}
else
{
PointScale(threadData);
}
while (finishCount < cores)
{
Thread.Sleep(1);
}
}
else
{
ThreadData threadData = new ThreadData(0, newHeight);
if (useBilinear)
{
BilinearScale(threadData);
}
else
{
PointScale(threadData);
}
}
tex.Resize(newWidth, newHeight);
tex.SetPixels(newColors);
tex.Apply();
Texture2D orig = new Texture2D(tex.width, tex.height);
if (flipV)
{
int xN = tex.width;
int yN = tex.width;
for (int i = 0; i < xN; i++)
{
for (int j = 0; j < yN; j++)
{
// tex.SetPixel(xN - i - 1, j, orig.GetPixel(i, j));
orig.SetPixel(i, yN - j - 1, tex.GetPixel(i, j));
}
}
orig.Apply();
}
else if (flipH)
{
int xN = tex.width;
int yN = tex.width;
for (int i = 0; i < xN; i++)
{
for (int j = 0; j < yN; j++)
{
// tex.SetPixel(xN - i - 1, j, orig.GetPixel(i, j));
orig.SetPixel(xN - i - 1, j, tex.GetPixel(i, j));
}
}
orig.Apply();
}
else
{
orig = tex;
}
return orig;
}
public static void BilinearScale(System.Object obj)
{
ThreadData threadData = (ThreadData)obj;
for (var y = threadData.start; y < threadData.end; y++)
{
int yFloor = (int)Mathf.Floor(y * ratioY);
var y1 = yFloor * w;
var y2 = (yFloor + 1) * w;
var yw = y * w2;
for (var x = 0; x < w2; x++)
{
int xFloor = (int)Mathf.Floor(x * ratioX);
var xLerp = x * ratioX - xFloor;
newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
y * ratioY - yFloor);
}
}
mutex.WaitOne();
finishCount++;
mutex.ReleaseMutex();
}
public static void PointScale(System.Object obj)
{
ThreadData threadData = (ThreadData)obj;
for (var y = threadData.start; y < threadData.end; y++)
{
var thisY = (int)(ratioY * y) * w;
var yw = y * w2;
for (var x = 0; x < w2; x++)
{
newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];
}
}
mutex.WaitOne();
finishCount++;
mutex.ReleaseMutex();
}
private static Color ColorLerpUnclamped(Color c1, Color c2, float value)
{
return new Color(c1.r + (c2.r - c1.r) * value,
c1.g + (c2.g - c1.g) * value,
c1.b + (c2.b - c1.b) * value,
c1.a + (c2.a - c1.a) * value);
}
}
OK,实用的小功能已做完,大家如果有问题可以给我留言,猿类要技术共享哦!
传送门:git@github.com:wuzhangwuzhang/UnityHeadIconSet.git
unity上传头像_Unity用户自定义圆角头像相关推荐
- unity上传头像_unity通过http上传图片到服务器
[实例简介] unity通过http上传图片到服务器,上传截屏图片到tomcat服务器 [实例截图] [核心代码] UnityToPhpSavePicture └── UnityToPhpSavePi ...
- Unity 上传自定义头像
使用NativeGallery插件 using System.Collections; using System.Collections.Generic; using UnityEngine; usi ...
- 头像上传html js版,javascript头像上传代码实例
上传头像: 相关关键词: ondragover(拖动元素在投放区内移动) ondrop (元素放在投放区触发但是要去处理浏览器默认事件的影响:ondragenter.ondragover) dataT ...
- phpcms 会员头像h5上传_使用elementui的头像上传组件报错
不知道是我前端的问题?还是后端的问题?请问这个组件默认就是post传值.对吧?是否需不需要再去设置请求协议什么的呢? `Access to XMLHttpRequest at 'http://11.1 ...
- php jquery ajax裁剪图照片,php+jquery+ajax无刷新图片上传裁切,模拟flash头像上传实例...
这几天自己在写一个cms.之前在用到图片上传裁切的时候总是用的flash的,或者是swfupload之类的.用的还不熟练,所以今天就用ajax做一个图片上传裁切的实例.个人感觉还不错,现在就分享出来. ...
- php flash 图片上传,php+jquery+ajax无刷新图片上传裁切,模拟flash头像上传实例
这几天自己在写一个cms.之前在用到图片上传裁切的时候总是用的flash的,或者是swfupload之类的.用的还不熟练,所以今天就用ajax做一个图片上传裁切的实例.个人感觉还不错,现在就分享出来. ...
- uni-app - 头像图片裁剪组件(支持多种裁剪,手势控制旋转或缩放、内外部控制图片移动、提供上传后端接口方案、头像图片美化)全端完美兼容 H5 App 小程序,最好用的图片上传后裁剪插件教程源代码
前言 网上的教程代码非常乱且都有 BUG 存在,非常难移植到自己的项目中,而且很难. 实现了 完美兼容 H5 App 小程序,选取手机本地相册或拍照,图片上传裁切内置多种方案,样式随便改, 本文代码干 ...
- Unity 上传下载随笔
Unity + NodeJs 上传下载文件 随笔(还木有实现完整) 安装node.js node -v 1.npm install connect serve-static 安装 2.npm inst ...
- unity上传头像_unity3d 上传本地PC图片
网上有两个常用的方法,我先用了第一种. 第一种方法: 需要注意三点,首先导入system.window.Forms.dll库,导进去后一定记得要修改playersetting下的optimizatio ...
最新文章
- Java的死锁的例子
- 我如何调优SQL Server查询
- 深圳职业技术学院计算机工程学院江学锋,毕业论文附属材料07013505刘丽.doc
- Spring集合 (List,Set,Map,Properties) 实例
- ZOJ 3228(AC自动机+修改的匹配)
- recyclerview 加载fragment_恢复 RecyclerView 的滚动位置
- Java中文乱码问题(转)
- 深度学习 autoencoder_笔记:李淼博士-基于模仿学习的机器人抓取与操控
- 搭建小程序表情包教程
- es6 字符串模板 随手记
- SpringCloud的Hystrix(二) 某消费者应用(如:ui、网关)访问的多个微服务的断路监控...
- vue引入自己写的js文件
- sql注入 java_JAVA实现sql注入点检测
- Win10 Edge浏览器设置默认bing/google为搜索引擎教程
- PDF转word的两种办法
- Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
- 一道关于SVM的机器学习作业题
- 提升ASO榜单排名优化主要方式有哪些?
- Oracle应用之to_char(参数,'FM990.00')函数
- sns是什么?可以做什么?
热门文章
- Rosetta Stone 罗赛塔 罗塞塔 石碑
- 关于Hystrix整合ribbon调用其他服务时 首次进入回退
- 【Python之GUI开发案例】用Python的tkinter开发聚合翻译神器
- Shell攻关之正则表达式
- android开发双击唤醒屏幕,安卓手机双击唤醒原理是什么 安卓双击唤醒原理介绍...
- hdu2094 c语言
- 【CentOS】常用命令
- CapstoneCS5262设计DP转HDMI4K60Hz+VGA1080P转换电路|DP to hdmi+vga转换器方案设计方法|CS5262Demoboard参考电路
- 同步和异步的区别和优缺点
- UTF-8 带BOM 和 UTF-8无BOM 的区别?