Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

htmlTexture plugin

Discussion in 'Made With Unity' started by bliprob, May 15, 2007.

  1. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    I'm happy to help you debug the problem, but you'll have to give the exact error messages you're seeing.

    The bundle *IS* the plugin. It's for both development and deployment. (Plugins don't work in the web player, by the way.) You must copy this into the project's "plugins" folder. If you don't have one, you need to create one in the project's "assets" directory.

    If the how-to steps are unclear, here's some more detail:

    1. In your project, create a new c# script. Rename the file "TextureHtml.cs". Open the script in your editor.

    2. Paste this into the file:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.InteropServices;
    6.  
    7. public class TextureHtml : MonoBehaviour {
    8.  
    9.     [DllImport ("htmlTexture")]
    10.     private static extern void htmlTexture_start( string url, int width, int height );
    11.  
    12.     [DllImport ("htmlTexture")]
    13.     private static extern void htmlTexture_stop();
    14.  
    15.     [DllImport ("htmlTexture")]
    16.     private static extern void htmlTexture_update ( int texID );
    17.  
    18.     [DllImport ("htmlTexture")]
    19.     private static extern void htmlTexture_setURL( string url);
    20.  
    21.         public int width =512;
    22.         public int height = 512;
    23.  
    24. void Start() {
    25.    m_Texture = new Texture2D (width, height, TextureFormat.ARGB32, false);
    26.    m_Texture.Apply();
    27.    htmlTexture_start("http://google.com", width, height);
    28.    // put the texture on something
    29.    Transform.renderer.sharedMaterial.mainTexture = m_Texture;
    30. }
    31.  
    32. void Update() {
    33.    htmlTexture_update( m_Texture.GetInstanceID() );
    34. }
    35.  
    36. void OnApplicationQuit() {
    37.    htmlTexture_stop();
    38. }
    39.  
    40. }
    41.  
    3. In the project, attach the script to an object. For now, I'd start simple -- create a new cube.


    That's just a few of the plugin functions, but it should be enough to get you started.
     
  2. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Thanks much bliprob. I realize providing tech support to a complete amateur is not in the job description of helping the community by providing a script. I very much appreciate your help.

    I've followed your instructions and get two errors.

    Assets/TextureHtml.cs(1) error: The name 'm_Texture' does not exist in the context of 'TextureHtml'

    and

    Assets/TextureHtml.cs(1) error: 'UnityEngine.Component.renderer': An object reference is required for the nonstatic field, method or property.

    If I try and drag the script onto a plane or any other object it tells me:

    "Can't add script
    Can't add script behaviour TextureHtml. you need to fix all compile errors in all scripts first."

    Any ideas?
     
  3. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Sorry about that. That's what I get for trying to cull the script down to its simplest bits at the end of the night, on a computer that doesn't have Unity installed!

    I think your script errors come from a variable I missed -- m_Texture. Add a declaration for m_Texture as below and you should be set.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.InteropServices;
    6.  
    7. public class TextureHtml : MonoBehaviour {
    8.  
    9.    [DllImport ("htmlTexture")]
    10.    private static extern void htmlTexture_start( string url, int width, int height );
    11.  
    12.    [DllImport ("htmlTexture")]
    13.    private static extern void htmlTexture_stop();
    14.  
    15.    [DllImport ("htmlTexture")]
    16.    private static extern void htmlTexture_update ( int texID );
    17.  
    18.    [DllImport ("htmlTexture")]
    19.    private static extern void htmlTexture_setURL( string url);
    20.  
    21.     public int width = 512;
    22.     public int height = 512;
    23.     private Texture2D m_Texture;
    24.    
    25.     void Start() {
    26.        m_Texture = new Texture2D (width, height, TextureFormat.ARGB32, false);
    27.        m_Texture.Apply();
    28.        htmlTexture_start("http://google.com", width, height);
    29.        // put the texture on something
    30.        Transform.renderer.sharedMaterial.mainTexture = m_Texture;
    31.     }
    32.    
    33.     void Update() {
    34.        htmlTexture_update( m_Texture.GetInstanceID() );
    35.     }
    36.    
    37.     void OnApplicationQuit() {
    38.        htmlTexture_stop();
    39.     }
    40.  
    41. }
    42.  
     
  4. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    The first error is gone, but still:

    Assets/TextureHtml.cs(1) error: 'UnityEngine.Component.renderer': An object reference is required for the nonstatic field, method or property.
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    The line that says
    Code (csharp):
    1.  
    2. Transform.renderer.sharedMaterial.mainTexture = m_Texture;
    Should simply be:
    Code (csharp):
    1.  
    2. renderer.sharedMaterial.mainTexture = m_Texture;
     
  6. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Sure enuff! Thnx!

    Could this be used on say a GUITexture object?
     
  7. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Or, is there a way to make this texture into a Self-Illum/Vertex Lit material?
     
  8. milkytreat

    milkytreat

    Joined:
    Jan 15, 2007
    Posts:
    267
    Holy zombie jesus, seriously this is fantastic!

    Things like this is the reason I always have to have a second pair of pants nearby.
     
  9. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Alrighty, I've given this a try in Unity 2.0 and it's working fine. (There's a weird issue with the texture coordinates not sticking across project loads that I'll be submitting.) Recent changes:

    - Multiple textures. You can use this plugin to render more than one URL to different textures. It keeps track of the various offscreen webviews internally.

    - Scrollbars. Finally remembered to hide the scrollbars.

    - Keypresses. You can pass a keystroke into the webview using htmlTexture_sendKeypress(). The plugin sends a keydown followed by a keyup to the associated webview.

    As for self-illuminated textures, just set the material to use the self-illum shader of your choice.
     
  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You're a rock star Rob, thanks!
     
  11. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Requisite screenshot. Each cube is a bookmark; each has a image of the HTML of the page it takes you to. I'm using the new 2.0 GUI controls at the top for back and URL to go to. (Had to throw shadows in there, too.)



    Even with three Flash pages playing videos in there, the playback fps is great.
     
  12. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Wow, that is really very cool. I've got to think of some great use for this!
     
  13. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    Wow, very nice Rob. good job ! :D
     
  14. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Here's a possible use: use a SWF to grab a webcam shot of the human player's face, and then place that texture onto a mesh in the game.

    Another is voice chat -- make a tiny SWF to grab the microphone and send the audio stream to a Red5 server, and play the other streams that are on the server.
     
  15. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Bingo Rob, your plugin opens many doors... Very nice work.
     
  16. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Wow, I just found this thread thanks to shaun who linked to it from another thread. This really is an incredibly cool plugin. It's kind of weird realizing that I didn't notice this while browsing the UnifyWiki.

    If I read it correctly, Windows support should be possible but is just not implemented, yet. Are there any plans on that? On the Wiki, Windows-support is not mentioned in the "to do" section...

    Also, I think this would be extremely cool for the Web player - are there any integration plans? The plugin itself only has 24kB, but I guess one problem might be that it relies on the (Apple?) webkit (which is probably not available "by default" on Windows :-( - but I believe there's some IE-COM-object that can probably used for the same purpose and is always available on that platform).

    Just yesterday, I watched the keynote from Unite 2007 and I think it's kind of funny how Unity might "take over the Web" with this feature integrated to the core ;-)

    Jashan
     
  17. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
  18. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Jashan, sorry I missed your comment. This is totally possible to do in Windows, using the exact same method I'm using with WebKit -- make an offscreen web control, and grab the bits stuff them into an OpenGL texture every render cycle. And I agree it's an obvious thing for Unity to have -- so much so that I never bothered to make the Windows version of the plugin, because I'd convinced myself that Unity 2.0 would have it! Obviously that wasn't the case, so I'll put the Windows version back on my to-do list.

    I don't know why it's not on the wiki. If you search for htmlTexture you'll find the page I made with the necessary interface to use the plugin. I guess it just needs a link from the plugins list.
     
  19. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Oh, I hope I didn't create confusion - I think it's actually linked on the Wiki, I just didn't notice it when I was browsing throught the Wiki. Not sure - I think if the Wiki supports images, it might be cool to add a screenshot for people like me who can't read ;-)

    Jashan
     
  20. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Hi Rob, thanks for your work on the plugin - it's very cool.
    I've hooked it up with Unity Networking and made a 'group' browser. The problem I'm having is that I can't seem to get the current URL back from htmlTexture.
    Would be good to have something like htmlTexture_getURL.

    Also, any idea's on when a Windows version might be up for testing?

    Cheers
    Shaun.
     

    Attached Files:

  21. dingosmoov

    dingosmoov

    Joined:
    Jul 12, 2005
    Posts:
    559
    Please make this part of the web player. The potential for rendering Google Ads inside your game is one good reason I can think of. Could help in generating revenue...
     
  22. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    The trick is making a good Windows version of the plugin. IIRC it's much much harder to do. They tried it for Global Conflicts.

    -Jon
     
  23. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Please indeed do consider this to be possible in windows too.

    I know most people are die hard MAC fans in here (And I must say I can't blame you, since I allready like my mac too and only have it for 4 months out of my 16 years of windows usage)


    This plugin opens a very huge number of doors for a lot of us.
     
  24. Loran

    Loran

    Joined:
    Nov 14, 2007
    Posts:
    124
    I found that kind of plugin VERY useful.
    It Rocks
    ;)
     
  25. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi guys,

    Rob, fine work, i've been trying to get your code from the wiki working, but I must be doing something wrong. People in the irc channel have suggested the plugin may not be updating the texture properly but im not sure why it woudlnt. I currently have this


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class PluginTexture : MonoBehaviour
    7. {
    8.     public int width = 512;
    9.     public int height = 512;
    10.  
    11. //  htmlTexture_start() creates the offscreen window and webview
    12. //  url: initial URL to display, use null for none
    13. //  width, height: currently must be a power of 2 (i.e. 128, 256, 512, 1024) due
    14. //    to my using GL_TEXTURE_2D
    15.  
    16. [DllImport ("htmlTexture")]
    17. private static extern void htmlTexture_start( string url, int width, int height);
    18.  
    19. // htmlTexture_stop() releases the window and webview
    20.  
    21. [DllImport ("htmlTexture")]
    22. private static extern void htmlTexture_stop();
    23.  
    24. //
    25. // htmlTexture_update() -- call this in your MonoBehavior::Update()
    26. // loads a bitmap of the webview into openGL with the given texture ID
    27. //
    28.  
    29. [DllImport ("htmlTexture")]
    30. private static extern void htmlTexture_update ( int texID );
    31.  
    32. //
    33. // browser functions
    34. //
    35. // as you'd expect, handles any URL, HTML and CSS that webkit can
    36. //
    37.  
    38. [DllImport ("htmlTexture")]
    39. private static extern void htmlTexture_setURL( string url);
    40.  
    41. [DllImport ("htmlTexture")]
    42. private static extern void htmlTexture_goBack();
    43.    
    44. [DllImport ("htmlTexture")]
    45. private static extern void htmlTexture_goForward();
    46.  
    47. // sends a string to the webview's javascript interpreter
    48. // returns a string ptr result -- use Marshal.PtrToStringAnsi() to read
    49.  
    50. [DllImport ("htmlTexture")]
    51. private static extern void htmlTexture_sendJavascript( string js);
    52.  
    53. //
    54. // these functions simulate a mouse event in the webview
    55. //
    56.  
    57. [DllImport ("htmlTexture")]
    58. private static extern void htmlTexture_mousemoved( int _x, int _y );
    59.  
    60. [DllImport ("htmlTexture")]
    61. private static extern void htmlTexture_mousedown( int _x, int _y );
    62.  
    63. [DllImport ("htmlTexture")]
    64. private static extern void htmlTexture_mouseup( int _x, int _y );
    65.  
    66. // htmlTexture_leftclick() = a mousemoved, mousedown, and mouseup
    67.  
    68. [DllImport ("htmlTexture")]
    69. private static extern void htmlTexture_leftclick( int _x, int _y );
    70.  
    71.     private Texture2D m_Texture;
    72.  
    73.     void Start()
    74.     {
    75.         m_Texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
    76.         m_Texture.Apply();
    77.         htmlTexture_start("http://google.com", width, height);
    78.  
    79.         // put the texture on something
    80.         transform.renderer.sharedMaterial.mainTexture = m_Texture;
    81.     }
    82.  
    83.     void Update()
    84.     {
    85.         htmlTexture_update(m_Texture.GetInstanceID());
    86.     }
    87.  
    88.     void OnApplicationQuit()
    89.     {
    90.         htmlTexture_stop();
    91.     }
    92.  
    93. void OnMouseUp()
    94. {
    95.     RaycastHit hit;
    96.     if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
    97.         int x = width - (int) (hit.textureCoord.x * width);
    98.         int y = height - (int) (hit.textureCoord.y * height);
    99.         htmlTexture_mouseup( x, y );
    100.  
    101.     }
    102. }
    103.  
    104. }
    But it registers no clicks.. what am I missing?

    any help much appreciated

    Regards

    Will[/code]
     
  26. Doug

    Doug

    Joined:
    Oct 19, 2007
    Posts:
    36
    I had a problem with it not registering clicks too, on debugging I found that the correct UVs were not being reported back. I kept getting (0,0).

    So technically the clicks were being registered, but they were all in the top left of the browser.

    I think the problem is related to what bliprob said at the end of page 4 of this thread:

    Didn't troubleshoot it much further since all my projects target windows anyway.
     
  27. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    So you didn't figure out why and theres no info from the man himself on this issue?

    Also how did you print the co-ordinates , assuming thats how u discovered they were just 0,0 ??
     
  28. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Sorry, will -- for some reason I didn't get an email when you posted to the thread. Maybe my spam filter is overacting. I haven't been up on these forums in a while. In fact, I was just jumping back in to let everyone know that I'm working on the Windows version of the plugin, finally. Cross your fingers.

    As for the coordinates, do you have a collider on the object you want to click on? I believe a Mesh collider is what's required.
     
  29. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Bliprob, what a nice news to hear that you are working on the WIN version of this system.

    This would be very powerfull if we could use it in our projects, so good luck with the windows version and keep us informed :)

    Regards,

    Bart Libert
    EducaSoft
     
  30. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi there thanks for getting back to me, i'm using a mesh collider now, but its still not registering clicks on any page i load on as a texture. Is there a way I can check where the clicks are hitting co-ordinate wise? then I could kind of check whats going on..
     
  31. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    will: Your code looks right. Here is the code I am using in the demo app.

    Code (csharp):
    1.  
    2. void OnMouseUp()
    3. {
    4.         RaycastHit hit;
    5.         if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
    6.             int x = width - (int) (hit.textureCoord.x * width);
    7.             int y = height - (int) (hit.textureCoord.y * height);
    8.             print("Object: " + hit.collider.gameObject.name +  ", HitPt: " + hit.point + ", Mouse: " + Input.mousePosition + ", textureCoord: " + hit.textureCoord + ", textureCoord2: " + hit.textureCoord2 + ", x: " + x + ", y: "+ y);
    9.             htmlTexture_mouseup( m_Texture.GetInstanceID(), x, y );
    10.         }
    11. }
    12.  
    My call to htmlTexture_mouseup() is different because I'm using the version of the plugin that supports multiple texture, but otherwise it looks the same.

    So stick that print() in there and let's see what it says.
     
  32. gazwi86

    gazwi86

    Joined:
    Jan 25, 2008
    Posts:
    21
    I've managed to get the texture working on a cube I've dropped into Unity. Im now trying to get mouse events to work so that the user can follow links but the code you've posted above isn't working.

    m_Texture.GetInstanceID(),

    returns "No overload for method 'htmlTexture_mouseup' takes '3' arguments"

    Love the plugin btw, quite inspiring...
     
  33. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Two new functions. One was requested by Shaun earlier in the thread; the other was an obvious thing to finish once I'd figured out Shaun's request:

    Code (csharp):
    1.  
    2. [DllImport ("htmlTexture")]
    3. private static extern void htmlTexture_getURL( int texID, StringBuilder url, int stringCapacity);
    4. // to get the current URL, you need to pass in a StringBuilder and its capacity (to
    5. // avoid buffer overflow errors)
    6.    
    7. [DllImport ("htmlTexture")]
    8. private static extern void htmlTexture_sendJavascriptWithReturn( int texID, string js, StringBuilder result, int stringCapacity);
    9. // sends a string to the webview's javascript interpreter
    10. // will return the returned javascript result in StringBuilder result
    11.  
    - getURL will return the current URL being displayed.

    - sendJavascriptWithResult will accept some javascript to be executed against the current page; anything returned by the javascript you call will be returned in the stringbuilder parameter.

    This means that you can communicate bi-directionally with a page: i.e. fill out and submit forms, perform ajax commands, get results from a Flash movie. For instance, here's a popular Mii generator that was going around the net. Drop this into a game, and using the new Javascript command, you could actually pull the description of the Mii and use it to build a 3D character from prefabs.

    Combining both of my plugins (htmlTexture and UniWii) allows a pretty nice simulation of an actual Wii!

     
  34. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Amazing work Rob - the sendJavascriptWithReturn command deserves some serious experimentation :D
    Thanks,
    Shaun
     
  35. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
  36. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Oops there weems to be nothing on the url you specified above.

    Also, how is the Windows version of the plugin going ?



    Kind regards,

    Bart
     
  37. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Sorry, I fixed the URL above.

    The windows version is coming along fine. Honestly, the biggest problem I faced was expanding my VMWare hard drive so that I could install the C++ compiler. What I thought would be trivial turned out to be complex. It sounds stupid, and it was. Anyway, I'm back to Win32 C++ now.

    For rendering, I am using an offscreen IE control. The result is, the plugin renders content differently on each platform: on Macs it looks like Safari, and on Windows it (obviously) looks like IE. HTML and CSS rendering quirks remain the same for each platform. It seemed like this was a sensible thing approach.

    But, it's totally possible to use WebKit on windows and get identical content rendering on each platform. And it also seems sensible to have a plugin that rendered pages identically (i.e. pixel-for-pixel) on both platforms, especially considering cases where you're mapping content to a complex mesh and those stray pixels matter.

    I don't know if anyone is actually doing this. Does anyone have an opinion?
     
  38. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Well, I must say what you say there sounds logic but also complex.

    I do know however that a htmlplugin would open a huge world for us, because its has endless possibilities.

    So as soon as you have something which can be tested (on the windows side I mean then) then please let me/us know :)
     
  39. dirkk0

    dirkk0

    Joined:
    Nov 29, 2007
    Posts:
    16
    @bliprob: both possibilities make sense.

    People might want to display websites that look best (whatever that means) on each platform. Others might want an identical look on each. So a flag 'use default browser/use webkit' (or gecko etc) would be great.
     
  40. dingosmoov

    dingosmoov

    Joined:
    Jul 12, 2005
    Posts:
    559
    CAN ANY BODY ON THE UNITY TEAM COMMENT ON MAKING THIS PART OF UNITY AND THE WEBPLAYER?!!!

    I MEAN ISN'T MOST OF THE WORK DONE ALREADY!
     
  41. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Ouch my ears
     
  42. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    There's something wrong with your shift key. Seriously, I'm not an employee of Unity but my earlier reply to you is really a good reason.

    -Jon
     
  43. dingosmoov

    dingosmoov

    Joined:
    Jul 12, 2005
    Posts:
    559
    With all due respect Jonathan, I read your reply, and am familiar with the Global Conflicts issue you mentioned.

    I apologize for the previous CAPS if that offended you. It was excited happy caps, sorry.

    The plugin is simply amazing! Is it possible someone on the Unity Team could look at the plugin code? Isn't this the type of functionality that you wanted in Global Conflicts?

    Maybe the work that has been done with this is farther along than what they tried to achieve in Global Conflicts.

    (I don't know if bliprob and Unity have already combined forces.)
     
  44. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    I just tested my windows bitmap-grabbing code and it takes 10 milliseconds to grab a bitmap from the hidden IE control and pass it to OpenGL. (Or perhaps less -- GetTickCount() isn't the highest resolution timer around.) That's plenty fast, more than fast enough for good playback of Flash animations. I think the framerate of the Windows version of the plugin will be as good as the Mac version.

    I don't know anything about Global Conflicts, or any issues that were faced with it, so I feel like I'm missing something here. (If you can elaborate, either here or privately, please feel free to do so.)

    Unity hasn't contacted me about this plugin (or even at all, except for a brief hello from Tom Higgins when I joined the community).

    If this was on Unity's roadmap, I think they'd have done it by now. It was not hard to do. The hard part for me was simply clearing the decks to be able to code; Unity wouldn't have that problem. That said, if Unity wants to slam this plugin into the product for everyone, I'm totally game and Tom knows where to find me. ;)
     
  45. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Its xtremely nice that you are willing to do all this.

    Maybe if its all working we could ask UT to include it so webplayers also get this capability. Until then a standalone version/solution is more then welcome.
     
  46. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I haven't tried the Mac plugin yet, but I will the first chance I get. Although I have no idea what I would use this plugin for, I think this is a very cool development and want to say a big THANKS for sharing it the the rest of us!
     
  47. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hey man, still having no luck whatsoever with this plugin, to break it down i've got his code -

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.InteropServices;
    6. using System.Text;
    7.  
    8. public class TexturePlayback : MonoBehaviour {
    9.        
    10.     [DllImport ("htmlTexture")]
    11.     private static extern void htmlTexture_start( int textID, int width, int height, string url );
    12.     //  creates the offscreen window and webview
    13.     //  url: initial URL to display, use null for none
    14.     //  width, height: currently must be a power of 2 (i.e. 128, 256, 512, 1024) due to my using GL_TEXTURE2D
    15.  
    16.     [DllImport ("htmlTexture")]
    17.     private static extern void htmlTexture_stop();
    18.     // releases all windows and webview; use on quit
    19.  
    20.     [DllImport ("htmlTexture")]
    21.     private static extern void htmlTexture_update ( int texID );
    22.     // makes bitmap of the webview and loads it into openGL with the given texture ID
    23.  
    24.     //
    25.     // browser functions
    26.     //
    27.     // as you'd expect, handles any URL, HTML and CSS that webkit can
    28.     //
    29.  
    30.     [DllImport ("htmlTexture")]
    31.     private static extern void htmlTexture_setURL( int texID, string url);
    32.  
    33.     [DllImport ("htmlTexture")]
    34.     private static extern void htmlTexture_getURL( int texID, StringBuilder url, int stringCapacity);
    35.     // to get the current URL, you need to pass in a StringBuilder and its capacity (to
    36.     // avoid buffer overflow errors)
    37.    
    38.     [DllImport ("htmlTexture")]
    39.     private static extern void htmlTexture_goBack( int texID );
    40.        
    41.     [DllImport ("htmlTexture")]
    42.     private static extern void htmlTexture_goForward( int texID );
    43.        
    44.     [DllImport ("htmlTexture")]
    45.     private static extern void htmlTexture_sendJavascript( int texID, string js);
    46.     // sends a string to the webview's javascript interpreter
    47.     // will return a string result as soon as I get the Marshall class figured out
    48.  
    49.     [DllImport ("htmlTexture")]
    50.     private static extern void htmlTexture_sendJavascript( int texID, string js, StringBuilder result, int stringCapacity);
    51.     // sends a string to the webview's javascript interpreter
    52.     // will return a string result
    53.  
    54.        
    55.     [DllImport ("htmlTexture")]
    56.     private static extern void htmlTexture_sendKeypress( int texID, string s);
    57.    
    58.    
    59.     //
    60.     // these functions simulate a mouse event in the webview
    61.     //
    62.  
    63.     [DllImport ("htmlTexture")]
    64.     private static extern void htmlTexture_mousemoved( int texID, int _x, int _y );
    65.    
    66.     [DllImport ("htmlTexture")]
    67.     private static extern void htmlTexture_mousedown( int texID,  int _x, int _y );
    68.  
    69.     [DllImport ("htmlTexture")]
    70.     private static extern void htmlTexture_mouseup( int texID, int _x, int _y );
    71.    
    72.     [DllImport ("htmlTexture")]
    73.     private static extern void htmlTexture_leftclick( int texID, int _x, int _y );
    74.     // leftclick = a mousemoved, mousedown, and mouseup
    75.  
    76. }
    77.  
    78.  
    79.  
    80. void OnMouseUp()
    81. {
    82.     RaycastHit hit;
    83.     if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
    84.         int x = width - (int) (hit.textureCoord.x * width);
    85.         int y = height - (int) (hit.textureCoord.y * height);
    86.         htmlTexture_mouseup(m_Texture.GetInstanceID(), x, y );
    87.     }
    88. }
    89.  
    on a cube and the error i've got is in the attached pic. Tried commenting it out that line and it still says the same thing so that confuses me! I am still a bit of a novice here so help me out!

    Cheers

    Will[/code]
     

    Attached Files:

  48. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    will: You've ended the TexturePlayback class too early, and your "OnMouseUp()" function is outside that class block. Move the final curly bracket for TexturePlayback from before the OnMouseUp function to after it, and it should compile.
     
  49. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi, thanks for the prompt reply, but now i have a new error - sorry about being dumb with the class, im used to JS so always forget the rule.

    See screengrab for new error - its not happy with the width, height and m_Texture specification.

    also, to be sure, here is latest code.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.InteropServices;
    6. using System.Text;
    7.  
    8. public class TexturePlayback : MonoBehaviour {
    9.        
    10.     [DllImport ("htmlTexture")]
    11.     private static extern void htmlTexture_start( int textID, int width, int height, string url );
    12.     //  creates the offscreen window and webview
    13.     //  url: initial URL to display, use null for none
    14.     //  width, height: currently must be a power of 2 (i.e. 128, 256, 512, 1024) due to my using GL_TEXTURE2D
    15.  
    16.     [DllImport ("htmlTexture")]
    17.     private static extern void htmlTexture_stop();
    18.     // releases all windows and webview; use on quit
    19.  
    20.     [DllImport ("htmlTexture")]
    21.     private static extern void htmlTexture_update ( int texID );
    22.     // makes bitmap of the webview and loads it into openGL with the given texture ID
    23.  
    24.     //
    25.     // browser functions
    26.     //
    27.     // as you'd expect, handles any URL, HTML and CSS that webkit can
    28.     //
    29.  
    30.     [DllImport ("htmlTexture")]
    31.     private static extern void htmlTexture_setURL( int texID, string url);
    32.  
    33.     [DllImport ("htmlTexture")]
    34.     private static extern void htmlTexture_getURL( int texID, StringBuilder url, int stringCapacity);
    35.     // to get the current URL, you need to pass in a StringBuilder and its capacity (to
    36.     // avoid buffer overflow errors)
    37.    
    38.     [DllImport ("htmlTexture")]
    39.     private static extern void htmlTexture_goBack( int texID );
    40.        
    41.     [DllImport ("htmlTexture")]
    42.     private static extern void htmlTexture_goForward( int texID );
    43.        
    44.     [DllImport ("htmlTexture")]
    45.     private static extern void htmlTexture_sendJavascript( int texID, string js);
    46.     // sends a string to the webview's javascript interpreter
    47.     // will return a string result as soon as I get the Marshall class figured out
    48.  
    49.     [DllImport ("htmlTexture")]
    50.     private static extern void htmlTexture_sendJavascript( int texID, string js, StringBuilder result, int stringCapacity);
    51.     // sends a string to the webview's javascript interpreter
    52.     // will return a string result
    53.  
    54.        
    55.     [DllImport ("htmlTexture")]
    56.     private static extern void htmlTexture_sendKeypress( int texID, string s);
    57.    
    58.    
    59.     //
    60.     // these functions simulate a mouse event in the webview
    61.     //
    62.  
    63.     [DllImport ("htmlTexture")]
    64.     private static extern void htmlTexture_mousemoved( int texID, int _x, int _y );
    65.    
    66.     [DllImport ("htmlTexture")]
    67.     private static extern void htmlTexture_mousedown( int texID,  int _x, int _y );
    68.  
    69.     [DllImport ("htmlTexture")]
    70.     private static extern void htmlTexture_mouseup( int texID, int _x, int _y );
    71.    
    72.     [DllImport ("htmlTexture")]
    73.     private static extern void htmlTexture_leftclick( int texID, int _x, int _y );
    74.     // leftclick = a mousemoved, mousedown, and mouseup
    75.  
    76.    
    77.     void OnMouseUp()
    78. {
    79.     RaycastHit hit;
    80.     if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
    81.         int x = width - (int) (hit.textureCoord.x * width);
    82.         int y = height - (int) (hit.textureCoord.y * height);
    83.         htmlTexture_mouseup(m_Texture.GetInstanceID(), x, y );
    84.     }
    85. }
    86.    
    87. }
    88.  
     

    Attached Files:

  50. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    will: it looks like the errors are all basic c# syntax errors. The compiler is complaining because you haven't declared the member variables width, height or m_Texture, which my sample OnMouseUp() function is using. You need to add:
    Code (csharp):
    1.    public int width = 512;
    2.    public int height = 512;
    3.    private Texture2D m_Texture;
    4.  
    Unfortunately, as it is now, you can't just copy and paste the two different chunks of code on the wiki and have it work; the wiki page assumes you'll declare those variables.

    I'll put a complete class on the wiki later. For now, here's what your code should look like:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class whateverYouCallIt : MonoBehavior {
    7.  
    8.    // paste the plugin interface here; your code did this right.
    9.  
    10.    // declare your member variables here. My example code uses:
    11.  
    12.    public int width = 512;
    13.    public int height = 512;
    14.    private Texture2D m_Texture;
    15.  
    16.    // Now, put in your class method overrides. You also did that right:
    17.  
    18.    void OnMouseUp() {
    19.       // whatever you want to do here
    20.    }
    21.  
    22.    // now, end the class
    23.  
    24. }
    25.