Unity3D研究院之Unity中连接本地或局域网MySQL数据库

  • 用户名
  • Email
开启辅助访问
用户名  找回密码
登录 注册帐号

扫一扫,访问微社区


</div>

<!-- /Block user information module HEADER -->
<ul class="p_pop nav_pop" id="umnav_menu" style="display: none;">

        <li><a href="http://www.manew.com/forum.php?mod=guide&amp;view=my" _style="background-image:url(http://www.manew.com/static/image/feed/thread_b.png) !important">帖子</a></li>

        <li><a href="http://www.manew.com/home.php?mod=space&amp;do=favorite&amp;view=me" _style="background-image:url(http://www.manew.com/static/image/feed/favorite_b.png) !important">收藏</a></li>

        <li><a href="http://www.manew.com/home.php?mod=magic" _style="background-image:url(http://www.manew.com/static/image/feed/magic_b.png) !important">道具</a></li>

        <li><a href="http://www.manew.com/home.php?mod=medal" _style="background-image:url(http://www.manew.com/static/image/feed/medal_b.png) !important">勋章</a></li>

        <li><a href="http://www.manew.com/home.php?mod=task" _style="background-image:url(http://www.manew.com/static/image/feed/task_b.png) !important">任务</a></li>

        <li><a href="http://www.manew.com/forum.php?mod=collection&amp;op=my" _style="background-image:url(http://www.manew.com/static/image/feed/collection_b.png) !important">淘帖</a></li>

        <li><a href="misc.php?mod=ranklist" _style="background-image:url(http://www.manew.com/static/image/feed/ranklist_b.png) !important">排行榜</a></li>
  <li><a href="http://www.manew.com/home.php?mod=spacecp" rel="nofollow">设置</a></li>

          <li><a href="member.php?mod=logging&amp;action=logout&amp;formhash=a237ee7e" rel="nofollow">退出</a></li>
</ul>
<div class="header_user" style="float: right;"><ul id="header_nav">
<li class="login_list"><a class="login_block_q" href="http://www.manew.com/tao">淘帖</a></li>
<li class="login_list"><a class="login_block_q" href="http://www.manew.com/thread-32240-1-1.html" rel="nofollow">微信签到</a></li>

</ul></div>
<div class="bus_search">    <div class="scbar_narrow cl" id="scbar">
帖子搜索

initSearchmenu('scbar', ''); jQuery(function(){ jQuery("#scbar #scbar_txt").focus(function(){ jQuery("#scbar").addClass("longer"); }).blur(function(){ jQuery("#scbar").removeClass("longer"); }); })
登录 后使用快捷导航
没有帐号? 注册帐号

签到成功!

您今天第{todayrank}个签到,签到排名竞争激烈,记得每天都来签到哦!

已连续签到: {constant}天,累计签到: {days}
开发者专栏

关注:1690

当前位置:游戏蛮牛 技术专区 开发者专栏

__________________________________________________________________________________
开发者干货区版块规则:

  1、文章必须是图文形式。(至少2幅图)
      2、文章字数必须保持在1500字节以上。(编辑器右下角有字数检查)
      3、本版块只支持在游戏蛮牛原创首发,不支持转载。
      4、本版块回复不得无意义,如:顶、呵呵、不错……【真的会扣分的哦】
      5、……
__________________________________________________________________________________



查看: 1335 | 回复: 3

发新帖

[雨松MOMO] Unity3D研究院之打开照相机与本地相册进行裁剪显示(三十三)


[复制链接]
 [移动端链接]


抢楼 抢楼 本帖为抢楼帖,欢迎抢楼! 

跳转到指定楼层



楼主

发表于 2015-8-19 10:35:21 | 只看该作者 | 只看大图 回帖奖励 | 阅读模式


雨松MOMO 当前离线
积分
2191
查看详细资料

排名
4688
昨日变化
6

199

主题

209

帖子

2191

积分

Rank: 9Rank: 9Rank: 9

UID
36308
好友
0
蛮牛币
5039
威望
0
注册时间
2014-7-25
在线时间
65 小时
最后登录
2017-5-4

专栏作家

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册帐号

x

         最近做项目需要用到这个功能,就是在Unity中调用本地相册或直接打开摄像机拍照并且裁剪一部分用于用户头像,今天研究了一下,那么研究出成果了MOMO一定要分享给大家。Unity与Android的交互还有谁不会?? 如果有不会的朋友请看MOMO之前的文章喔,Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)这里有关交互的方式就不详细说明,主要将如何在Unity中打开摄像机、在Unity中打开本地相册,选一个照片后如何进行裁剪,最后将图片转换成Texture显示在U3D的世界当中。

首先看看Eclipse中的Android插件部分,我的包名是com.xys请大家与MOMO保持一致,Unity工程中也需要是这个包名噢。

UnityTestActivity.java 这个类是Unity的插件主类,在这里调用是打开摄像机 还是本地相册的方法。

[AppleScript] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< font face = "Verdana," > package com.xys;
  
import android. content .Context;
import android. content .Intent;
import android.os.Bundle;
import com.unity 3 d.player.UnityPlayerActivity;
  
public class UnityTestActivity extends UnityPlayerActivity {
/ / public class UnityTestActivity extends Activity {
  
     Context mContext = null;
  
     @Override
     public void onCreate ( Bundle savedInstanceState ) {
         super.onCreate ( savedInstanceState ) ;
         mContext = this;
     }
  
     / / Unity中会调用这个方法,用于区分打开摄像机 开始本地相册
      public void TakePhoto ( String str )
      {
              Intent intent = new Intent ( mContext , WebViewActivity. class ) ;
              intent.putExtra ( "type" , str ) ;
              this.startActivity ( intent ) ;
      }
}
< / font >

然后是WebViewActivity.java 这里主要处理用户打开摄像机或本地相册后如何进行裁剪图片,并且把裁剪的图片储存在本地文件中。

[AppleScript] 纯文本查看 复制代码
?
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
< font face = "Verdana," > package com.xys;
  
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
  
import com.unity 3 d.player.UnityPlayer;
  
import android.app.Activity;
import android. content .Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android. view .KeyEvent;
import android.widget.ImageView;
  
public class WebViewActivity extends Activity
{
  
     ImageView imageView = null; 
  
     public static final int NONE = 0 ;
     public static final int PHOTOHRAPH = 1 ; / / 拍照
     public static final int PHOTOZOOM = 2 ; / / 缩放
     public static final int PHOTORESOULT = 3 ; / / 结果 
  
     public static final String IMAGE_UNSPECIFIED = "image/*"
  
     public final static String FILE_NAME = "image.png" ;
     public final static String DATA_URL = "/data/data/" ;
  
     @Override
     protected void onCreate ( Bundle savedInstanceState ) {
  
         super.onCreate ( savedInstanceState ) ;
  
         setContentView ( R.layout. main ) ;
  
         imageView = ( ImageView ) this.findViewById ( R. id .imageID ) ;
  
         String type = this.getIntent ( ) .getStringExtra ( "type" ) ;
  
         / / 在这里判断是打开本地相册还是直接照相
         if ( type. equals ( "takePhoto" ) )
         {
               Intent intent = new Intent ( MediaStore.ACTION_IMAGE_CAPTURE ) ;
               intent.putExtra ( MediaStore.EXTRA_OUTPUT , Uri.fromFile ( new File ( Environment.getExternalStorageDirectory ( ) , "temp.jpg" ) ) ) ;
               startActivityForResult ( intent , PHOTOHRAPH ) ;
         } else
         {
                Intent intent = new Intent ( Intent.ACTION_PICK , null ) ;
                intent.setDataAndType ( MediaStore.Images.Media.EXTERNAL_CONTENT_URI , IMAGE_UNSPECIFIED ) ;
                startActivityForResult ( intent , PHOTOZOOM ) ;
         }
  
     } 
  
     @Override
     protected void onActivityResult ( int requestCode , int resultCode , Intent data ) {
         if ( resultCode = = NONE )
             return ;
         / / 拍照
         if ( requestCode = = PHOTOHRAPH ) {
             / / 设置文件保存路径这里放在跟目录下
             File picture = new File ( Environment.getExternalStorageDirectory ( ) + "/temp.jpg" ) ;
             startPhotoZoom ( Uri.fromFile ( picture ) ) ;
         } 
  
         if ( data = = null )
             return
  
         / / 读取相册缩放图片
         if ( requestCode = = PHOTOZOOM ) {
             startPhotoZoom ( data .getData ( ) ) ;
         }
         / / 处理结果
         if ( requestCode = = PHOTORESOULT ) {
             Bundle extras = data .getExtras ( ) ;
             if ( extras ! = null ) { 
  
                 Bitmap photo = extras.getParcelable ( "data" ) ;
                 imageView.setImageBitmap ( photo )
  
                 try {
                     SaveBitmap ( photo ) ;
                 } catch ( IOException e ) {
                     / / TODO Auto - generated catch block
                     e.printStackTrace ( ) ;
                 }
             } 
  
         } 
  
         super.onActivityResult ( requestCode , resultCode , data ) ;
     } 
  
     public void startPhotoZoom ( Uri uri ) {
         Intent intent = new Intent ( "com.android.camera.action.CROP" ) ;
         intent.setDataAndType ( uri , IMAGE_UNSPECIFIED ) ;
         intent.putExtra ( "crop" , "true" ) ;
         / / aspectX aspectY 是宽高的比例
         intent.putExtra ( "aspectX" , 1 ) ;
         intent.putExtra ( "aspectY" , 1 ) ;
         / / outputX outputY 是裁剪图片宽高
         intent.putExtra ( "outputX" , 300 ) ;
         intent.putExtra ( "outputY" , 300 ) ;
         intent.putExtra ( "return-data" , true ) ;
         startActivityForResult ( intent , PHOTORESOULT ) ;
     } 
  
     public void SaveBitmap ( Bitmap bitmap ) throws IOException {
  
         FileOutputStream fOut = null;
  
         / / 注解 1
         String path = "/mnt/sdcard/Android/data/com.xys/files" ;
         try {
               / / 查看这个路径是否存在,
               / / 如果并没有这个路径,
               / / 创建这个路径
               File destDir = new File ( path ) ;
               if ( !destDir. exists ( ) )
               {
  
                   destDir.mkdirs ( ) ;
  
               }
  
             fOut = new FileOutputStream ( path + "/" + FILE_NAME ) ;
         } catch ( FileNotFoundException e ) {
             e.printStackTrace ( ) ;
         }
         / / 将Bitmap对象写入本地路径中,Unity在去相同的路径来读取这个文件
         bitmap.compress ( Bitmap.CompressFormat.PNG , 100 , fOut ) ;
         try {
             fOut.flush ( ) ;
         } catch ( IOException e ) {
             e.printStackTrace ( ) ;
         }
         try {
             fOut. close ( ) ;
         } catch ( IOException e ) {
             e.printStackTrace ( ) ;
         }
     }
  
     @Override
     public boolean onKeyDown ( int keyCode , KeyEvent event )
     {
         if ( keyCode = = KeyEvent.KEYCODE_BACK & & event .getRepeatCount ( ) = = 0 )
         {
                / / 当用户点击返回键是 通知Unity开始在 "/mnt/sdcard/Android/data/com.xys/files" ;路径中读取图片资源,并且现在在Unity中
                UnityPlayer.UnitySendMessage ( "Main Camera" , "messgae" , FILE_NAME ) ;
  
         }
         return super.onKeyDown ( keyCode , event ) ;
     }
} < / font >

注解1:主要是路径”/mnt/sdcard/Android/data/com.xys/files”,如下图所示,我们在这里把文件保存在这个路径下。为什么要把图片2进制文件写在这里呢? 还记得以前MOMO给大家说过在Unity中访问Android或IOS本地2进制文件时用到的这个路径,Application.persistentDataPath   该路径等价于 /mnt/sdcard/Android/data/com.xys/files ,当然后者的包名是对应的工程包名,这样在Unity中可以找到对应裁剪后的图片文件,并且显示在Unity中。

屏幕快照-2012-07-13-下午11.10.02.png (150.7 KB, 下载次数: 0)

下载附件  保存到相册

2014-8-19 10:22 上传

AndroidManifest.xml  这个文件也没什么好说的,大家看看吧。

[AppleScript] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
< manifest xmlns : android = "http://schemas.android.com/apk/res/android"
     package = "com.xys"
     android : versionCode = "1"
     android : versionName = "1.0" >
  
     
  
     < application
         android : icon = "@drawable/ic_launcher"
         android : label = "@string/app_name" >
         < activity
             android : name = ".UnityTestActivity"
             android : label = "@string/app_name" >
             
                 
  
                 
             
         
  
        < activity
             android : name = ".WebViewActivity" >
         
     
  
     
     
     
 
 
 
< / activity
< / activity
< / application

然后把上面的Android工程打包做成插件放在Unity中。如下图所示,这个我的Unity工程中对应的路径。如果看不懂的朋友请看我之前的文章哈。

屏幕快照-2012-07-13-下午11.17.01.png (27.88 KB, 下载次数: 0)

下载附件  保存到相册

2014-8-19 10:31 上传

然后看Test.cs脚本,它直接挂在摄像机身上。

[AppleScript] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using UnityEngine;
using System.Collections;
using System.IO;
  
public class Test : MonoBehaviour
{
     public GUISkin skin;
     Texture texture;
  
     void Update ( )
     {
  
         if ( Input.GetKeyDown ( KeyCode.Escape ) || Input.GetKeyDown ( KeyCode.Home ) )
           {
                Application.Quit ( ) ;
           }
     }
  
     void OnGUI ( )
     {
          GUI.skin = skin;
  
         if ( GUILayout.Button ( "打开手机相册" ) )
         {
              / / 调用我们制作的Android插件打开手机相册
              AndroidJavaClass jc = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) ;
                 AndroidJavaObject jo = jc.GetStatic ( "currentActivity" ) ;
              jo.Call ( "TakePhoto" , "takeSave" ) ;
  
         }
         if ( GUILayout.Button ( "打开手机摄像机" ) )
         {
              / / 调用我们制作的Android插件打开手机摄像机
              AndroidJavaClass jc = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) ;
                 AndroidJavaObject jo = jc.GetStatic ( "currentActivity" ) ;
              jo.Call ( "TakePhoto" , "takePhoto" ) ;
  
         }
         if ( texture ! = null )
         {
             / / 注意! 我们在这里绘制Texture对象,该对象是通过
             / / 我们制作的Android插件得到的,当这个对象不等于空的时候
             / / 直接绘制。
             GUI.DrawTexture ( new Rect ( 100 , 300 , 300 , 300 ) , texture ) ;
         }
     }
  
     void  messgae ( string str )
     {
         / / 在Android插件中通知Unity开始去指定路径中找图片资源
         StartCoroutine ( LoadTexture ( str ) ) ;
  
     }
  
   IEnumerator LoadTexture ( string name )
   {
     / / 注解 1
     string path  =  "file://" + Application.persistentDataPath + "/" + name ;
  
     WWW www = new WWW ( path ) ;
     while ( !www.isDone )
     {
  
     }
     yield return www;
     / / 为贴图赋值
     texture = www.texture;
   }
}

注解1:请大家一定要注意这个路径的写法, 前面一定要加 “File://” 不然无法读取。OK说了这么多我们看看这个项目运行的效果,激动人心的时刻来临啦 嚯嚯嚯嚯!!!

1.首次进入的画面, 这里的图片是我刚刚从相册选择的


屏幕快照-2012-07-13-下午11.32.17.png (164.87 KB, 下载次数: 0)

下载附件  保存到相册

2014-8-19 10:34 上传


2.打开相册我们选择一张图片

屏幕快照-2012-07-13-下午11.32.41.png (439.37 KB, 下载次数: 0)

下载附件  保存到相册

2014-8-19 10:34 上传


3. 选择一张图片,我们进行裁剪


屏幕快照-2012-07-13-下午11.33.02.png (285.21 KB, 下载次数: 0)

下载附件  保存到相册

2014-8-19 10:34 上传


最后我们返回到Unity中界面。新的图片Unity已经完成读取,界面上已经修改成刚刚我裁剪的啦,哇咔咔。 怎么样,还不错啦? 哈哈后。这个做用户头像肯定给力 蛤蛤。


屏幕快照-2012-07-13-下午11.33.19.png (163.63 KB, 下载次数: 0)

下载附件  保存到相册

2014-8-19 10:34 上传


如果点击打开摄像机按钮,拍照完毕后会提示裁剪,裁减完毕返回到Unity界面中同样能看到效果。
最后MOMO将本篇博文的源码放出来,  源码包括Android的工程 与Unity的工程,雨松MOMO祝大家学习愉快,大家互相学习互相进步,加油哇咔咔,啦啦啦。

下载地址:http://vdisk.weibo.com/s/ac5ZB

unity3d 打开相册;unity3d 图片 裁剪;unity 裁剪图片;unity bitmap;unity 调用相册;unity bitmap 转texture;unity3d 相册;unity3d的图片裁剪;unity 图片裁剪;bitmap unity;unity 调用本地相册;unity texture bitmap;unity bitmap texture

AR/VR

untiy博客







width="728" height="90" src="//rcm-cn.amazon-adsystem.com/e/cm?t=manew-23&o=28&p=48&l=ez&f=ifr&f=ifr" border="0" scrolling="no">
aimgcount[290626] = ['44031','44030','44029','44028','44027','44023']; attachimggroup(290626); var aimgfid = 0;

沙发
发表于 2015-12-23 11:40:40 | 只看该作者


yangke1981 当前离线
积分
1980
查看详细资料

排名
518
昨日变化
2

27

主题

537

帖子

1980

积分

Rank: 7Rank: 7Rank: 7Rank: 7

UID
2429
好友
5
蛮牛币
2932
威望
0
注册时间
2013-8-22
在线时间
270 小时
最后登录
2015-10-6

社区QQ达人

支持 雨松MOMO


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值